Solved

How to save outpuit from powershell into *.csv

Posted on 2011-09-08
3
395 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 69

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

821 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