Solved

Powershell extraction issues

Posted on 2013-01-31
6
227 Views
Last Modified: 2013-02-20
I have following below PS script which I used to run this in PS program.
The "AllServers.txt" have around 200 server name list, when I run the scripts it saved the file but it takes last server name which I have in the list , why this happens ?


Set-Location "E:\MicroTest\"
$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 "e:\MicroTest\AllServerVersions.csv"


Below is an example of the saved output.

"Server","Version","SP"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","9.00.4035.00","SP3"
"PrimaTest1122","8.00.2039","SP4"
"PrimaTest1122","8.00.2055","SP4"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.3042.00","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","9.00.4220.00","SP3"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","9.00.4220.00","SP3"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.4285.00","SP3"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","9.00.5000.00","SP4"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
"PrimaTest1122","10.0.4000.0","SP2"
0
Comment
Question by:motioneye
6 Comments
 
LVL 13

Assisted Solution

by:stergium
stergium earned 50 total points
ID: 38842790
Hello.
I first thought would be to change this
  $obj = $set.Tables[0] | select-object @{n = 'Server'; e = {$svr}}, Version, SP
to
  $obj = $set.Tables[0] | select-object @{n = $svr; e = {$svr}}, Version, SP

it should give you in any line the name of the server selected
please feed back
0
 
LVL 40

Assisted Solution

by:footech
footech earned 50 total points
ID: 38843448
I don't use PS w/ SQL, so I'm unfamiliar with a few of the commands in your script, but here's what I see is happening (kind of feeling my way through this).  In each loop of the foreach statement, the $obj variable is defined and it's value set (replacing what was previously set).  After the foreach completes all of its loops, $obj will have the last value that was assigned, which you then pipe to Export-CSV.  You either need to:
1) write to the file during every loop of the foreach, or
2) add the info to a variable and then after the foreach pipe all that accumulated data to Export-CSV.
Option 2 is usually the better way to go as it avoids having to open and close the file again and again (the open being by far the slowest operation).  I'm assuming that the rest of your script works just fine, just not the export to CSV part.  Since $obj is a table, I don't see any way around this except to either have the server name inserted/retrieved as part of the table, or deal with each row one at a time.

Set-Location "E:\MicroTest\"
$cmd = "SELECT SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"
$set = new-object system.data.dataset
$obj = @()
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].rows[-1] | select-object @{n = 'Server'; e = {$svr}}, Version, SP
}
$obj | export-csv -NoTypeInformation "e:\MicroTest\AllServerVersions.csv"

Open in new window

I'm totally guessing about the syntax to access the last row in the table ($set.Tables[0].rows[-1]) so forgive me if it doesn't work, but maybe it will give you other ideas.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 400 total points
ID: 38844092
The basic thoughts of footech are correct. $set will collect all results retrieved accumulated in $set.Tables[0], but contain no info about the server. However, $set.Tables[0].rows[-1] does not work (results in no output).

My suggestion is to enrich the DataSet with the server name, let it collect all info, and output it directly to Export-CSV:
Set-Location 'E:\MicroTest\'
$cmd = "SELECT SERVERPROPERTY('ServerName') as Server, 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
}
$set.Tables[0] | export-csv -NoTypeInformation 'e:\MicroTest\AllServerVersions.csv'

Open in new window

This should be the fastest and least memory-consuming method available.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38892260
You should not accept just all posts. Please make sure only helpful comments receive points, and to split the points according to the amount of help provided.

http:#a38842790 doesn't change anything to the good, it makes things worse. No points should be given for that.

http:#a38843448 adds some valid info, and though the code is not really working that way, it is only a small nuance failing. Accepting this comment is correct.

But http:#a38844092 shows the only working code, and since that is not based on posts of other Experts it should receive the major share of points.
0
 

Author Closing Comment

by:motioneye
ID: 38912495
Thanks, all of you have at least helped and give some ideas, of course the best answer will be awarded more, and sorry for the mistakes on awarding points
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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