How to use windpos power shell to extract all server information?

motioneye
motioneye used Ask the Experts™
on
I'm goofling around and found this scripts PS which I can use to extract all servers with sql information, but I have no idea whta is actually this syntax does, how should I run this scripts? Yes I;ve installed PS in my machine, tried run it, but I dont get any output from the servername which has been input into allservers.txt

# SQLVer.ps1

# usage: ./SQLVer.ps1

# Check SQL version

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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hello,

you need to save this script on your machine, put your SQL server names (or IP address) in a file in C:\data\AllServers.txt (you can also change the path in the script)

Then launch a powershell console, go where you put the .PS1 script and type :
./yourscriptname.ps1

or you can copy/paste the code directly in the console. Normally it will do the job.

You can also use the SQL cmdlet described here :

http://technet.microsoft.com/en-us/library/cc281847.aspx

Author

Commented:
Hi
I run it again but failed as below, the server already in the AllServers.txt


PS C:\Datat> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Datat


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/17/2009   4:28 PM         41 AllServers.txt
-a---         7/17/2009   4:13 PM        483 check.ps1


PS C:\Datat> check.ps1
The term 'check.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
At line:1 char:9
+ check.ps1 <<<<
PS C:\Datat>
Commented:
To launch a script with powershell , type :

./Check.ps1

or simply type the beginning of your script name and hit tab

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial