0

I wrote a small powershell script that reads name of sql server instances from text-file then loops them and output results of the query to the text file.

foreach ($Instance in Get-Content C:\temp\SQLServers.txt)
{
$Instance;
$File ="c:\temp\SqlLoopLog.txt"
Add-Content $File "`r`n$Instance"
Invoke-Sqlcmd -ServerInstance $Instance -Database msdb -Query "select * from dbo.[sysjobs]" | Out-File $File 
}

But when called Invoke-Sqlcmd the content of the file SqlLoopLog.txt is re-set each time. But if I add -append then the information is output in one row instead of the several rows and it is un-readable. How to output data in readable format?

Helen Dyakonova
  • 129
  • 1
  • 4

1 Answers1

0

can you try :

$output=(Invoke-Sqlcmd -ServerInstance $Instance -Database msdb -Query "select * from dbo.[sysjobs]").tostring()
$output+="`n"
$output | out-file -append $file
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103