Running ps script from task scheduler

Welcome Forums General PowerShell Q&A Running ps script from task scheduler

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #81064

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    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

      Participant
      Points: 1
      Rank: Member

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

The topic ‘Running ps script from task scheduler’ is closed to new replies.