May 10, 2011 at 7:07 am
I have written a script that will loop to get the SQL Server version on multiple servers/instances but it will not output all to a csv file. I am pretty sure it is the foreach command. My script is below. Any help would be appreciated.
foreach ($svr in Get-Content "C:\DBAScripts\SQLServers.txt")
{
$con = "server=$svr;database=master;Integrated Security=sspi"
$cmd = "select serverproperty('servername') as Name,serverproperty('productversion') as Version,serverproperty('productlevel') ServicePack,serverproperty('edition') as Edition"
$da = New-Object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
$dt = New-Object System.Data.DataTable
$da.fill($dt)
$dt | Export-csv C:\DBAScripts\Test.csv
}
May 10, 2011 at 11:11 am
Try this:
$dt | select-object Name,Version,ServicePack,Edition | Export-csv C:\DBAScripts\Test.csv -NoTypeInformation
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
May 10, 2011 at 1:56 pm
I had my question answered so I thought I would post it here for everyone else.
# Must call the name of the file as below
# C:\ PS > .\GetSQLServerVersions3.ps1 | Export-Csv -NoTypeInformation C:\DBAScripts\Test2.csv
foreach ($svr in Get-Content "C:\DBAScripts\SQLServers.txt")
{
$con = "server=$svr;database=master;Integrated Security=sspi"
$cmd = "select serverproperty('servername') as Name,serverproperty('productversion') as Version,serverproperty('productlevel') ServicePack,serverproperty('edition') as Edition"
$da = New-Object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
$dt = New-Object System.Data.DataTable
$da.fill($dt) | out-null
Write-Output $dt
}
May 10, 2011 at 2:09 pm
Yep, that works as well.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy