Forum Discussion

Ameerm2023's avatar
Ameerm2023
Copper Contributor
Apr 27, 2023

Not getting the values in CSV

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:

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)"

 

  • Andres-Bohren's avatar
    Andres-Bohren
    Steel Contributor

    Hi Ameerm2023 

    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 highest privileges (Basically: Run as Administrator)
    You can try to run the Script "with highest privileges"

    Did you select "Run wehther user is logged on or not"?

    Regards

    Andres

     

    • Ameerm2023's avatar
      Ameerm2023
      Copper Contributor

      Andres-Bohren 

       

       

      I am using the same account for other tasks scheduler and even in the same folder i am getting other records are getting saved but i dont know why this script is having the issue 

      • Andres-Bohren's avatar
        Andres-Bohren
        Steel Contributor
        Did you set a Path in Actions > "Start in" on your Scheduled Task?
        Otherwise try to set a Full Path for your CSV's in your Script: C:\Script\Output.csv

        Regards
        Andres
  • Axel Andersen's avatar
    Axel Andersen
    Copper Contributor
    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. Then you can run the task without elevated privileges.



  • AndySvints's avatar
    AndySvints
    Steel Contributor

    Hello Ameerm2023,

    Just my two cents on how to make your script more efficient. You are calling Get-MgUser 4 times, although 2 would be more than enough (Actually it can be achieved with just one but we have to start somewhere). Basically you can cut the execution time in half:

    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' -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}}
    
    Write-Host "Enabled Users: $($allEnabledUsers.Count)"
    
    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}}
    
    Write-Host "Disabled Users: $($allDisabledUsers.Count)"
    
     $allEnabledUsers | Export-CSV -NoTypeInformation $enabled
    
    Write-Host "CSV file for Enabled Users exported at $(Get-Date)"
    
    $allDisabledUsers | Export-Csv -NoTypeInformation $Disabled
    
    Write-Host "CSV file for Disabled Users exported at $(Get-Date)"
    
    Disconnect-MgGraph
    
    Write-Host "Script finished at $(Get-Date)"

    Hope that helps.

    • Ameerm2023's avatar
      Ameerm2023
      Copper Contributor
      Thanks for update and i have changed the scripts but still i am getting any value of i run the task scheduler but if i run it manually its getting saved.please help

Resources