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?
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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)"
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?
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–
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