CSV not getting saved

Anonymous
2023-04-27T11:06:09.26+00:00

I have the PowerShell scripts which is giving output saved as CSV file if i run the scripts manually but If create the windows scheduler to run it automatically i am not getting any result saved in the CSV file. Its empty

please advise whats the error

below is the scripts:

Write-Host "Script started at $(Get-Date)"

$Disabled = ".\UserUsage\DisabledUsers.csv"
$enabled = ".\UserUsage\EnabledUsers.csv"

Connect-MgGraph -ClientId "123" -TenantId "123" -CertificateThumbprint "123"
  
Sleep -Seconds 10

$allEnabledUsers = Get-MgUser -All -Filter 'accountEnabled eq true' 

Write-Host "Enabled Users: $($allEnabledUsers.Count)"

$allDisabledUsers = Get-MgUser -All -Filter 'accountEnabled eq false' 

Write-Host "Disabled Users: $($allDisabledUsers.Count)"

Sleep -Seconds 10

$allEnabledUsers = Get-MgUser -All -Filter 'accountEnabled eq true' -Property id, FirstName, LastName, Jobtitle, displayName, CompanyName, State, OfficeLocation, department, signInActivity, userPrincipalName,userType, createdDateTime, accountEnabled, passwordPolicies, mail, lastPasswordChangeDateTime, Phone | select id, displayName, FirstName, LastName, Jobtitle, CompanyName, State, OfficeLocation, Phone, department, userPrincipalName, userType, createdDateTime, accountEnabled, mail, lastPasswordChangeDateTime, passwordPolicies, @{N='LastSignInDateTime';E={$_.signInActivity.LastSignInDateTime}}, @{N='LastNonInteractiveSignInDateTime';E={$_.signInActivity.LastNonInteractiveSignInDateTime}} | Export-CSV -NoTypeInformation $enabled

Write-Host "CSV file for Enabled Users exported at $(Get-Date)"

Sleep -Seconds 10

$allDisabledUsers = Get-MgUser -Filter 'accountEnabled eq false' -All -Property id, FirstName, LastName, Jobtitle, displayName, CompanyName, State, OfficeLocation, department, signInActivity, userPrincipalName, userType, createdDateTime, accountEnabled, passwordPolicies, mail, lastPasswordChangeDateTime, Phone | select id, displayName, FirstName, LastName, Jobtitle, CompanyName, State, OfficeLocation, Phone, department, userPrincipalName, userType, createdDateTime, accountEnabled, mail, lastPasswordChangeDateTime, passwordPolicies, @{N='LastSignInDateTime';E={$_.signInActivity.LastSignInDateTime}}, @{N='LastNonInteractiveSignInDateTime';E={$_.signInActivity.LastNonInteractiveSignInDateTime}} | Export-Csv -NoTypeInformation $Disabled

Write-Host "CSV file for Disabled Users exported at $(Get-Date)"

Sleep -Seconds 10

Disconnect-MgGraph

Write-Host "Script finished at $(Get-Date)"

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-04-27T14:48:44.31+00:00

    Does it work if you change the Export-CSV to use named parameters?

    Export-CSV -Path $enabled -NoTypeInformation
    
    

    Does the account under which the scheduled task runs have permission to create a file in the locations you're referring to?


  2. Limitless Technology 44,746 Reputation points
    2023-04-28T14:27:55.04+00:00

    Hello there,

    Did you run the Interactive Script "As Administrator"? So the Files might have a Flag the User can't overwrite if he is not running with the highest privileges

    You could try to use a fully qualified path for the files:

    $Root = "C:\EveryoneCanWriteHere\UserUsage" #<- set ACL on this folder

    $Disabled = "$Root\DisabledUsers.csv"

    $enabled = "$Root\EnabledUsers.csv"

    Tweak the ACL if needed to allow the account running the task the correct access.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2023-05-01T14:50:47.9133333+00:00

    There are a few possibilities for the CSV not being written. But I think the most likely cause is that you've omitted the "Start in" value in the "Actions" part of the scheduled task and there's no directory named "UserUsage" in whatever is the current directory when the script is run.

    Try using the Start-Transcript cmdlet and some additional Write-Host cmdlets to track the progress of your scripts execution. You might also consider the comments I left in your original code.

    You should find the transcript in the TEMP folder.

    Start-Transcript -Path C:\Temp\Transcript.txt
    Write-Host "Script started at $(Get-Date)"
    
    Write-Host "The current directory ('.') is '$((pwd.Path))'"
    $Disabled = ".\UserUsage\DisabledUsers.csv"     # what is the current working directory????
    $enabled = ".\UserUsage\EnabledUsers.csv"
    
    Connect-MgGraph -ClientId "123" -TenantId "123" -CertificateThumbprint "123"
    Start-Sleep -Seconds 10                         # what purpose does Start-Sleep serve in a scheduled task??
    
    $allEnabledUsers = Get-MgUser -All -Filter 'accountEnabled eq true' 
    Write-Host "Enabled Users: $($allEnabledUsers.Count)"
    
    $allDisabledUsers = Get-MgUser -All -Filter 'accountEnabled eq false' 
    Write-Host "Disabled Users: $($allDisabledUsers.Count)"
    Start-Sleep -Seconds 10
    
    # Export-CSV doesn't return anything! Why assign the results to $allEnabledUsers???
    $allEnabledUsers = Get-MgUser -All -Filter 'accountEnabled eq true' -Property id, FirstName, LastName, Jobtitle, displayName, CompanyName, State, OfficeLocation, department, signInActivity, userPrincipalName,userType, createdDateTime, accountEnabled, passwordPolicies, mail, lastPasswordChangeDateTime, Phone | 
        Select-Object id, displayName, FirstName, LastName, Jobtitle, CompanyName, State, OfficeLocation, Phone, department, userPrincipalName, userType, createdDateTime, accountEnabled, mail, lastPasswordChangeDateTime, passwordPolicies, @{N='LastSignInDateTime';E={$_.signInActivity.LastSignInDateTime}}, @{N='LastNonInteractiveSignInDateTime';E={$_.signInActivity.LastNonInteractiveSignInDateTime}} |
            Export-CSV -NoTypeInformation $enabled
    
    Write-Host "CSV file for Enabled Users exported at $(Get-Date)"
    Start-Sleep -Seconds 10
    
    # Export-CSV doesn't return anything! Why assign the results to $allDisabledUsers???
    # Also . . . you already HAVE the didabled users in the $allDisabledUsers variable. Why not add the -Properties parameter to the previous Get-MgUser?
    $allDisabledUsers = Get-MgUser -Filter 'accountEnabled eq false' -All -Property id, FirstName, LastName, Jobtitle, displayName, CompanyName, State, OfficeLocation, department, signInActivity, userPrincipalName, userType, createdDateTime, accountEnabled, passwordPolicies, mail, lastPasswordChangeDateTime, Phone | 
        Select-Object id, displayName, FirstName, LastName, Jobtitle, CompanyName, State, OfficeLocation, Phone, department, userPrincipalName, userType, createdDateTime, accountEnabled, mail, lastPasswordChangeDateTime, passwordPolicies, @{N='LastSignInDateTime';E={$_.signInActivity.LastSignInDateTime}}, @{N='LastNonInteractiveSignInDateTime';E={$_.signInActivity.LastNonInteractiveSignInDateTime}} | 
            Export-Csv -NoTypeInformation $Disabled
    Write-Host "CSV file for Disabled Users exported at $(Get-Date)"
    
    Start-Sleep -Seconds 10
    
    Disconnect-MgGraph
    Write-Host "Script finished at $(Get-Date)"
    Stop-Transcript
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.