question

RTURNER avatar image
RTURNER asked

poweshell script wont succeed from sql agent

My script will only return the 1st server in the serverlist.txt $QueryPath= "D:\SQLQueries\BackupsCompleted.sql" $OutputFile = "D:\Outputs\backupscompleted.txt" $ExecuteQuery= Get-Content -path $QueryPath | out-string "Results -- > `r`n`r`n" > $OutputFile FOREACH($server in GC "D:\Serverlists\ServerList.txt") { $server >> $OutputFile "---------------------------------------------------------------------------------------------------------" >> $OutputFile invoke-sqlcmd -ServerInstance $server -query $ExecuteQuery -querytimeout 65534 | ft -autosize | out-string -width 4096 >> $OutputFile }
sqlpowershellsqlserveragent
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Mart avatar image
Mart answered
I usually put the servers in a CSV list then use Import-CSV. In the CSV simply have the heading then list the servers to check. You can then import the CSV i.e #gather list of servers $Servers = Import-CSV "D:\Serverlists\ServerList.csv" #invoke sql on each server FOREACH($server in $Servers) {...... Think that should do it for you :)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.