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

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