Scrip to connect to remote windows box to get SQL version

  • Dear All,

    I am very much new to power shell scripting where i need to find out what version of SQL Server is running on it and on which port the DB is running on the remote windows box.Could you pls help us to script this out in powershell.

    Regards,

    Gangadhara

  • Here is something close to what I use to get version information. not sure what you would have to do in order to get the ports SQL is listening on. The switch statement was put together really quickly just now, so you may want to add another check on minor version if major version is 10 do differentiate between SQL Server 2008 and SQL Server 2008 R2.

    #Add SQL PS Snapins

    $snapinTest = get-pssnapin | where-object {$_.name -like "*sql*"}

    if ($snapinTest.length -eq $null)

    {

    get-pssnapin -registered | add-pssnapin

    }

    #Load SMO assembly

    $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

    $sqlInstances = get-content "c:\scripts\commonvalues\sql.txt" #List of SQL instances, one per line. Add \DEFAULT if it is the default instance

    $errorActionPreference = "Stop"

    $pushFail = $false

    foreach ($sql in $sqlInstances)

    {

    if (($sql.substring(($sql.indexof("\") + 1), $sql.length - $sql.indexof("\") - 1)) -eq "default")

    {

    $sql2 = $sql.substring(0, $sql.length - ($sql.length - $sql.indexof("\")))

    }

    else

    {

    $sql2 = $sql

    }

    $sqlVersion = new-object "Microsoft.SqlServer.Management.Smo.Server" $sql2

    $ver = $sqlVersion.version.major

    switch ($ver)

    {

    9 {write-host "$sql2 is SQL 2005"}

    10 {write-host "$sql2 is SQL 2008/2008 R2"}

    11 {write-host "$sql2 is SQL 2012"}

    default {write-host "$sql2 is pre-SQL 2005"}

    }

    }

    Joie Andrew
    "Since 1982"

  • To get the SQL Server info,

    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") > $null

    $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'SQLName'

    #You can select all the properties

    $server

    #Just version and edition and language

    $server | select version, edition,language

    Or we can create a reusable function :

    Function Get-SqlServerInfo {

    [CmdletBinding()]

    param(

    [Parameter(Position=0, Mandatory=$true, ValueFromPipeline = $true,ValuefromPipelinebyPropertyName=$true)] [string[]]$SQLServer

    )

    begin {

    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") > $null

    }

    process {

    foreach ($SQL in $SQLServer) {

    $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $SQL

    write-output $server

    }

    }

    }

    and use

    Server1 | Get-SQLServerInfo

    "server'","server2" | Get-SQLServerInfo

    Get-SQLServerInfo Server1

    having a txt file with the names of the servers

    get-content c:\sqlnames.txt | Get-SQLServerInfo

    or passing as array. The parameter sqlserver in the functions is a string[], then I can use

    Get-SQLServerInfo -sqlserver (Get-content c:\sqlnames.txt)

    To select the properties just pipe to select

    Get-SQLServerInfo -sqlserver (Get-content c:\sqlnames.txt) | select version, edition,language

    TO THE PORT :

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null

    $WMI = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') 'SQLName'

    $WMI.ClientProtocols | select displayname -ExpandProperty ProtocolProperties

    PS - only support in SQL Server 2005 or higher and must have a SQL instance installed locally:-)

    OR in the WMI Directly

    Get-WmiObject -query "select * from ClientNetworkProtocolProperty" -Namespace root\Microsoft\SqlServer\ComputerManagement11 -ComputerName YourComputer

    Or reading the Registry :

    SQL Server 2000

    Default instance

    hklm:\SOFTWARE\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\TCP

    Named instance

    hklm:\SOFTWARE\Microsoft\Microsoft SQL Server(InstanceName)\MSSQLServer\SuperSocketNetLib\TCP

    SQL Server 2005

    hklm:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.X\MSSQLServer\SuperSocketNetLib\TCP\IPAll

    X is the number assigned to the instance. Default Instance is MSSQLSERVER

    SQL Server 2008/R2 2012

    Default instance

    hklm:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLX.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\TCP\IPAll

    Named instance

    hklm:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLX.(InstanceName)\MSSQLServer\SuperSocketNetLib\TCP\IPAll

    Change X to the version 10 or 11

    Choose you key and run remotely :

    $Registry = "hklm:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQLServer\SuperSocketNetLib\TCP\IPAll"

    invoke-command {param($Registry); Get-Itemproperty -path $Registry } -ComputerName YourComputer -ArgumentList $Registry

    $hell your Experience !!![/url]

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply