x

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
  }
more ▼

asked Jul 17 at 03:26 PM in Default

avatar image

RTURNER
1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Jul 18 at 02:58 PM

avatar image

Mart
529 1 6 10

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1149
x67
x1

asked: Jul 17 at 03:26 PM

Seen: 21 times

Last Updated: Jul 18 at 02:58 PM

Copyright 2017 Redgate Software. Privacy Policy