Running ps script from task scheduler

This topic contains 2 replies, has 2 voices, and was last updated by  Noel 2 weeks ago.

  • Author
    Posts
  • #81064

    Noel
    Participant

    I have the following script:

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
    $sqlServerNames = "computername"
    [Array]$Collection = foreach ($sqlServerName in $sqlServerNames) {
        $sqlServer = New-Object Microsoft.SqlServer.Management.Smo.Server($sqlServerName)
    
        foreach ($job in $sqlServer.JobServer.Jobs) {
            $job | Select-Object Name, OriginatingServer, OwnerLoginName, IsEnabled, LastRunDate, LastRunOutcome, DateCReated, DateLastModified, NextRunDate
        }
        $Collection += $job
    }
    $Collection | Export-CSV "\\computername\c$\\sqljobs.csv" -force –noType -Encoding:UTF8
    
    

    When i run this in powershell, I've no problems. However when i run via task scheduler with admin rights with the following parameters: -NoLogo -NonInteractive -File "\\computername\c$\Powershell\SF3.ps1"
    The csv file just comes up blank.

    Any ideas? Thank you.

  • #81068

    Dudebro
    Participant

    Stab-in-the-dark theory: The credentials used to run the scheduled task do not have login permissions to the SQL Server instance and the script fails to return any data.

    Do you use the same credentials for the scheduled task as you use to run the script manually?

    • #81071

      Noel
      Participant

      Yep, you're right. That's great, thank you!
      Not sure how I didn't see that.

You must be logged in to reply to this topic.