Created
July 15, 2022 00:58
-
-
Save SweetAsNZ/17fe1562523c6dc1b1148c8592a23bc6 to your computer and use it in GitHub Desktop.
Get SQL Servers Instances and Ports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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