SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scrip to connect to remote windows box to get SQL version


Scrip to connect to remote windows box to get SQL version

Author
Message
Gangadhara MS
Gangadhara MS
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 840
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
Joie Andrew
Joie Andrew
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2279 Visits: 2032
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"
Laerte Poltronieri...
Laerte Poltronieri Junior-367636
Old Hand
Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)Old Hand (325 reputation)

Group: General Forum Members
Points: 325 Visits: 836
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 !!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search