Solved

How to save outpuit from powershell into *.csv

Posted on 2011-09-08
3
390 Views
Last Modified: 2012-05-12
I have following powershell scripts, where it will scan server name from AllServers.txt, around 300 servers. Do you gusy knwo how to construct powershell to save the output into csv format ? in the same C:\ data folder ?


foreach ($svr in get-content "C:\data\AllServers.txt")
{
  $con = "server=$svr;database=master;Integrated Security=sspi"
  $cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"
  $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
}
0
Comment
Question by:motioneye
3 Comments
 
LVL 7

Assisted Solution

by:celazkon
celazkon earned 166 total points
ID: 36501707
Generally, to redirect the output to file instead of scripting console, use the out-file cmdlet.
For exact usage and info about it, see:
http://technet.microsoft.com/en-us/library/ee176924.aspx
0
 
LVL 10

Accepted Solution

by:
wls3 earned 167 total points
ID: 36502147
Unless you format the object to work with the Export-Csv cmdlet (Get-Help Export-Csv -Examples), the Out-File cmdlet (Get-Help Out-File -examples), mentioned above, will be your more effective approach.
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 36502293
Set-Location "C:\temp\ee"
$cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"
$set = new-object system.data.dataset
foreach ($svr in get-content "AllServers.txt")
{
  $constr = "server=$svr;database=master;Integrated Security=sspi"
  (new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $constr)).Fill($set) | out-null
  $obj = $set.Tables[0] | select-object @{n = 'Server'; e = {$svr}}, Version, SP 
}
$obj | export-csv -NoTypeInformation "C:\temp\ee\AllServerVersions.csv"

Open in new window

Is one way to do that. $obj would allow for further processing, if needed.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now