Link to home
Start Free TrialLog in
Avatar of computerstreber
computerstreberFlag for United States of America

asked on

PowerShell Output Log

I have created a PowerShell script to run a script on several SQL Servers. When I execute the PowerShell script, it displays the results of the first TSQL script, but doesn't show the results of the other TSQL scripts. Does anyone know how I can get this to show all output from the sql script?

Here is the output I am getting...

Serv1

Column1                                                                                            
-------                                                                                            
Microsoft SQL Server 2005 - 9.00.3073.00 (X64) ...    

Serv2

D0319D01

Column1                                                                                            
-------                                                                                            
Microsoft SQL Server 2005 - 9.00.3073.00 (X64) ...  

Here is the code in the script1.sql

SELECT @@VERSION

use database1;

SELECT TOP 10 * FROM table1

use database2;

SELECT TOP 10 * FROM table1

use database2;

SELECT TOP 10 * FROM table1
 
#Here is my code: 
 
$cmd = get-content "C:\script1.sql"
 
foreach ($svr in get-content "C:\serv.txt")
{
  $con = "server=$svr;database=master;Integrated Security=sspi"
  $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
  $dt = new-object System.Data.DataTable
  $da.fill($dt) | out-null
  $svr
  $dt | Format-Table -autosize
}

Open in new window

Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Try to move the format-table outside of the foreach
$cmd = get-content "C:\script1.sql"
 
@(foreach ($svr in get-content "C:\serv.txt")
{
  $con = "server=$svr;database=master;Integrated Security=sspi"
  $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
  $dt = new-object System.Data.DataTable
  $da.fill($dt) | out-null
  $svr
  $dt 
}) | format-table

Open in new window

Avatar of computerstreber

ASKER

didn't work...
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial