Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to save outpuit from powershell into *.csv

Posted on 2011-09-08
3
Medium Priority
?
406 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 664 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 668 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 71

Assisted Solution

by:Qlemo
Qlemo earned 668 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

885 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