I am trying to run the same SQL query against a number of databases and output the result to a separate text file for each database. What I'm doing so far has issues with the output file name. When I ammend filename to just use C:\TEMP\usersoutput_$Num.txt
, it puts everything in to just one file. Here is my code:
$DBArray = @("DataBase1","DataBase2","DataBase3","DataBase4","DataBase5")
$Num = 0
foreach ($element in $DBArray) {
(Invoke-Sqlcmd -Query "...." -ServerInstance Server2 -Database $DBArray[$Num] -Verbose
) *>> C:\TEMP\usersoutput_$DBArray[$Num].txt |
foreach {$Num = $Num + 1}
}
Where am I going wrong with this; is this the best way to accomplish this?