Author Posts

October 2, 2017 at 11:55 am

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.

October 2, 2017 at 1:01 pm

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?

October 2, 2017 at 1:14 pm

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