Skip to content

Instantly share code, notes, and snippets.

@SweetAsNZ
Created July 15, 2022 00:58
Show Gist options
  • Save SweetAsNZ/17fe1562523c6dc1b1148c8592a23bc6 to your computer and use it in GitHub Desktop.
Save SweetAsNZ/17fe1562523c6dc1b1148c8592a23bc6 to your computer and use it in GitHub Desktop.
Get SQL Servers Instances and Ports
function Get-SQLInstancesPort
{
param ([string]$Server)
# REF: https://docs.microsoft.com/en-us/answers/questions/486485/get-a-list-of-instances-and-and-port-numbers-runni.html
# $Server = 'SQL1' # TESTING
[system.reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")|Out-Null
[system.reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement")|Out-Null
$mc = new-object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $Server
$Instances = $mc.ServerInstances
foreach ($Instance in $Instances) {
$port = @{Name = "Port"; Expression = {$_.ServerProtocols['Tcp'].IPAddresses['IPAll'].IPAddressProperties['TcpPort'].Value}}
$Parent = @{Name = "Parent"; Expression = {$_.Parent.Name}}
$Instance|Select $Parent, Name, $Port
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment