quicksqlserver

Web Hosting

Techtracker

Job Search

Quick sql server

Powered by TypePad

« SQL Best Practices in less than 6 minutes 1/3 | Main | Manipulating Registry with T-SQL »

August 27, 2008

SQL Server Version with Powershell

In running a large SQL Server environment , it is necessary to have as many timesaving methods as possible. For instance , I'm upgrading loads of sql servers at the moment and the following script , gives me a quick way of getting a summary of versions. Place all the relevant servers into a file , such as "C:\servers.txt" , just place them on separate lines.

An example of the lists in that document is:

SERVER1\INST1
SERVER2\INST2
etc

Keep in mind, you will need to have  Allow Remote Connections enabled

To run , copy and paste straight into a Powershell cmdlet ,which is a series of commands, usually more than one line, stored in a text file with a .ps1 extension.

-----------------------------CODE START-------------------------------------------------------------

foreach ($svr in get-content "C:\Servers.txt"){
    $dt = new-object "System.Data.DataTable"
    $cn = new-object System.Data.SqlClient.SqlConnection "server=$svr;database=master;Integrated Security=sspi"
    $cn.Open()
    $sql = $cn.CreateCommand()
    $sql.CommandText = "SELECT @@SERVERNAME AS ServerName, SERVERPROPERTY('ProductVersion') AS Version, SERVERPROPERTY('ProductLevel') as SP"
    $rdr = $sql.ExecuteReader()
    $dt.Load($rdr)
    $cn.Close()
    $dt | Format-Table -autosize
}

-----------------------------CODE END-------------------------------------------------------------

Powershell jobs

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d834539b6d69e200e5548d75478834

Listed below are links to weblogs that reference SQL Server Version with Powershell:

Comments

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Author: Jack Vamvas is a full time SQL Server DBA Developer based in the UK. He specialises in Tuning,Optimisation,T-SQL Programming,SSIS ,Database design and is also involved in a fullrange of SQL Server tasks. Currently, he is working on ITjobfeed , which gathers the latest IT vacancies and makes them available to job seekers.