Home Forums Programming Powershell Scrip to connect to remote windows box to get SQL version RE: Scrip to connect to remote windows box to get SQL version

  • 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"