Greetings. I'm a DBA and pretty new to PS. What I want to do is determine which SQL instances a query should be ran on based on a query from a specified SQL instance.
This instance houses an inventory table, and I want to pull a list of instances to run another query on. So the first query determines which instances to run the second query on.
So this works, but I have to supply a list of instances I want the main query to be ran on in a text file named si.txt:
$filePath = 'C:\Users\myUsername\PS\' $inputFile = $filePath + 'sqlCMD2.txt' $fileOut = $filePath + 'output.csv' foreach ($serverInstance in (Get-Content 'C:\Users\myUsername\PS\si.txt')) { Invoke-Sqlcmd -ServerInstance $serverInstance -InputFile $inputFile }
Content of sqlCMD2.txt:
select @@servername, getdate()
Content of si.txt:
machine1\instanceName
machine2\instanceName
That's no fun, I want it to look more like:
$filePath = 'C:\Users\myUsername\PS\' $inputFile = $filePath + 'sqlCMD2.txt' $fileOut = $filePath + 'output.csv' foreach ($serverInstance in (Get-Content 'C:\Users\myUsername\PS\queryServers.txt')) { Invoke-Sqlcmd -ServerInstance $serverInstance -InputFile $inputFile }
Content of queryServers.txt:
select servername from myServer.myDB.dbo.allServers where servername is not NULL and environment = 'dev' group by servername
So rather than directly inputting what servers to run sqlCMD.txt on, it would query the list of servers to run them on by running queryServers.txt.
Any ideas?
Thanks in advance! ChrisRDBA