@@ -1309,10 +1309,16 @@ function Find-SqlServerService {
13091309 $ServiceIpAddress = $null
13101310 $DomainName = $null
13111311 $ServiceTypeName = $null
1312- $ServiceName = $null
1312+ $ManagedComputerServerInstanceName = $null
13131313 $Port = $null
13141314 $IsDynamicPort = $false
13151315 $ServiceStartDate = $null
1316+ $Protocol = $null
1317+ $NetbiosComputerName = if ($ComputerName.IndexOf (' .' ) -gt 15 ) {
1318+ $ComputerName.Substring (0 , 15 )
1319+ } else {
1320+ $ComputerName.Split (' .' )[0 ]
1321+ }
13161322
13171323 $StdRegProv = $null
13181324 $RegistryKeyRootPath = $null
@@ -1337,9 +1343,11 @@ function Find-SqlServerService {
13371343 if (($_.Name ).IndexOf(' $' ) -gt 0 ) {
13381344 $InstanceName = ($_.Name ).Substring(($_.Name ).IndexOf(' $' ) + 1 )
13391345 $IsNamedInstance = $true
1346+ $ManagedComputerServerInstanceName = $InstanceName
13401347 } else {
13411348 $InstanceName = $null
13421349 $IsNamedInstance = $false
1350+ $ManagedComputerServerInstanceName = $_.Name
13431351 }
13441352
13451353 # Try and determine if this is a clustered server (and the FQDN & IP for the cluster if it is)
@@ -1388,83 +1396,114 @@ function Find-SqlServerService {
13881396 default { $_.Type.ToString () }
13891397 }
13901398
1399+ # Gather protocol details for SQL Server service
1400+ # Not applicable to other service types
1401+ $ServiceIpAddress = $null
1402+ $Port = $null
1403+ $IsDynamicPort = $null
1404+ $ServiceProtocol = $null
13911405
1392- # Get the port number for SQL Server Services
1393- # if ($_.Type -eq $ManagedServiceTypeEnum::SqlServer) {
13941406 if ($ServiceTypeName -ieq ' SQL Server' ) {
13951407
1396- $ServiceIpAddress = $null
1397- $Port = $null
1398- $IsDynamicPort = $null
1399- $ServiceName = if ($IsNamedInstance -eq $true ) { $InstanceName } else { $_.Name }
1400- $ManagedComputer.ServerInstances | Where-Object { $_.Name -ieq $ServiceName } | ForEach-Object {
1408+ # Gather protocol details
1409+ $ServiceProtocol = $ManagedComputer.ServerInstances | Where-Object { $_.Name -ieq $ManagedComputerServerInstanceName } | ForEach-Object {
1410+ $_.ServerProtocols | Where-Object { -not [String ]::IsNullOrEmpty($_.Name ) } | ForEach-Object {
1411+ New-Object - TypeName PSObject - Property @ {
1412+ Name = $_.Name
1413+ DisplayName = $_.DisplayName
1414+ IsEnabled = $_.IsEnabled
1415+ IPAddresses = $_.IPAddresses | Where-Object { $_.IPAddress } | ForEach-Object {
1416+ New-Object - TypeName PSObject - Property @ {
1417+ Name = $_.Name
1418+ IpAddress = $_.IPAddress.ToString ()
1419+ IpAddressFamily = $_.IPAddress.AddressFamily.ToString ()
1420+ IPAddressProperties = $ (
1421+ $IpAddressProperty = @ {}
1422+ $_.IPAddressProperties | Where-Object {
1423+ -not [String ]::IsNullOrEmpty($_.Name )
1424+ } | ForEach-Object {
1425+ $IpAddressProperty += @ { $_.Name = $_.Value }
1426+ }
1427+ Write-Output $IpAddressProperty
1428+ )
1429+ }
1430+ }
1431+ ProtocolProperties = $ (
1432+ $ProtocolProperty = @ {}
1433+ $_.ProtocolProperties | Where-Object {
1434+ -not [String ]::IsNullOrEmpty($_.Name ) -and
1435+ $_.Name -ine ' Enabled'
1436+ } | ForEach-Object {
1437+ $ProtocolProperty += @ { $_.Name = $_.Value }
1438+ }
1439+ Write-Output $ProtocolProperty
1440+ )
1441+ }
1442+ }
1443+ }
14011444
1402- $_.ServerProtocols | Where-Object {
1403- $_.Name -ieq ' tcp' -and
1404- $_.IsEnabled -eq $true
1405- } | ForEach-Object {
1445+ # Determine an IP Address & port the SQL Server service is listening on (if TCP/IP enabled)
1446+ $ServiceProtocol | Where-Object {
1447+ $_.Name -ieq ' tcp' -and
1448+ $_.IsEnabled -eq $true
1449+ } | ForEach-Object {
14061450
1407- # If listening on all IPs then get the "IPAll" port info
1408- # Otherwise get port info for an IP that's enabled and active
1451+ # If listening on all IPs then get the "IPAll" port info
1452+ # Otherwise get port info for an IP that's enabled and active
14091453
1410- if ($_.ProtocolProperties [' ListenOnAllIPs' ].Value -eq $true ) {
1454+ if ($_.ProtocolProperties [' ListenOnAllIPs' ] -eq $true ) {
14111455
1412- $_.IPAddresses | Where-Object { $_.Name -ieq ' ipall' } | ForEach-Object {
1413- $Port = $_.IPAddressProperties [' TcpPort' ].Value
1414- if (-not $Port ) {
1415- $Port = $_.IPAddressProperties [' TcpDynamicPorts' ].Value
1416- $IsDynamicPort = $true
1417- } else {
1418- $IsDynamicPort = $false
1419- }
1456+ $_.IPAddresses | Where-Object { $_.Name -ieq ' ipall' } | ForEach-Object {
1457+ $Port = $_.IPAddressProperties [' TcpPort' ]
1458+ if (-not $Port ) {
1459+ $Port = $_.IPAddressProperties [' TcpDynamicPorts' ]
1460+ $IsDynamicPort = $true
1461+ } else {
1462+ $IsDynamicPort = $false
14201463 }
1464+ }
14211465
1422- } else {
1466+ } else {
14231467
1424- # Start with 127.0.0.1 first in case that's the only IP that's enabled
1425- $_.IPAddresses | Where-Object {
1426- $_.IPAddressProperties [' Active' ].Value -eq $true -and
1427- $_.IPAddressProperties [' Enabled' ].Value -eq $true -and
1428- $_.IPAddress.AddressFamily -ieq ' InterNetwork' -and
1429- $_.IPAddress -ieq ' 127.0.0.1'
1430- } | Select-Object - First 1 | ForEach-Object {
1468+ # Start with 127.0.0.1 first in case that's the only IP that's enabled
1469+ $_.IPAddresses | Where-Object {
1470+ $_.IPAddressProperties [' Active' ] -eq $true -and
1471+ $_.IPAddressProperties [' Enabled' ] -eq $true -and
1472+ $_.IPAddressFamily -ieq ' InterNetwork' -and
1473+ $_.IPAddress -ieq ' 127.0.0.1'
1474+ } | Select-Object - First 1 | ForEach-Object {
14311475
1432- $ServiceIpAddress = $_.IPAddress.ToString ()
1433- $Port = $_.IPAddressProperties [' TcpPort' ].Value
1476+ $ServiceIpAddress = $_.IPAddress
1477+ $Port = $_.IPAddressProperties [' TcpPort' ]
14341478
1435- if (-not $Port ) {
1436- $Port = $_.IPAddressProperties [' TcpDynamicPorts' ].Value
1437- $IsDynamicPort = $true
1438- } else {
1439- $IsDynamicPort = $false
1440- }
1479+ if (-not $Port ) {
1480+ $Port = $_.IPAddressProperties [' TcpDynamicPorts' ]
1481+ $IsDynamicPort = $true
1482+ } else {
1483+ $IsDynamicPort = $false
14411484 }
1485+ }
14421486
1443- # Now try and see if there's a non-loopback IP enabled
1444- $_.IPAddresses | Where-Object {
1445- $_.IPAddressProperties [' Active' ].Value -eq $true -and
1446- $_.IPAddressProperties [' Enabled' ].Value -eq $true -and
1447- $_.IPAddress.AddressFamily -ieq ' InterNetwork' -and
1448- $_.IPAddress -ine ' 127.0.0.1'
1449- } | Select-Object - First 1 | ForEach-Object {
1487+ # Now try and see if there's a non-loopback IP enabled
1488+ $_.IPAddresses | Where-Object {
1489+ $_.IPAddressProperties [' Active' ] -eq $true -and
1490+ $_.IPAddressProperties [' Enabled' ] -eq $true -and
1491+ $_.IPAddressFamily -ieq ' InterNetwork' -and
1492+ $_.IPAddress -ine ' 127.0.0.1'
1493+ } | Select-Object - First 1 | ForEach-Object {
14501494
1451- $ServiceIpAddress = $_.IPAddress.ToString ()
1452- $Port = $_.IPAddressProperties [' TcpPort' ].Value
1495+ $ServiceIpAddress = $_.IPAddress
1496+ $Port = $_.IPAddressProperties [' TcpPort' ]
14531497
1454- if (-not $Port ) {
1455- $Port = $_.IPAddressProperties [' TcpDynamicPorts' ].Value
1456- $IsDynamicPort = $true
1457- } else {
1458- $IsDynamicPort = $false
1459- }
1460- }
1461- }
1498+ if (-not $Port ) {
1499+ $Port = $_.IPAddressProperties [' TcpDynamicPorts' ]
1500+ $IsDynamicPort = $true
1501+ } else {
1502+ $IsDynamicPort = $false
1503+ }
1504+ }
14621505 }
1463-
14641506 }
1465- } else {
1466- $Port = $null
1467- $IsDynamicPort = $null
14681507 }
14691508
14701509
@@ -1502,17 +1541,46 @@ function Find-SqlServerService {
15021541 if ($IsClusteredInstance -eq $true ) {
15031542 [String ]::Join(' \' , @ ($ClusterName , $InstanceName ))
15041543 } else {
1505- [String ]::Join(' \' , @ ($ComputerName , $InstanceName ))
1544+ if (
1545+ $ServiceTypeName -ine ' SQL Server' -or
1546+ $ (
1547+ $ServiceProtocol | Where-Object {
1548+ $_.Name -ine ' sm' -and
1549+ $_.IsEnabled -eq $true
1550+ }
1551+ )
1552+ ) {
1553+ # Another protocol besides shared memory is enabled or the service isn't SQL Server\SQL Server Agent; use the FQDN
1554+ [String ]::Join(' \' , @ ($ComputerName , $InstanceName ))
1555+ } else {
1556+ # Shared memory is the only protocol enabled; use the NETBIOS name
1557+ [String ]::Join(' \' , @ ($NetbiosComputerName , $InstanceName ))
1558+ }
15061559 }
15071560 } else {
15081561 if ($IsClusteredInstance -eq $true ) {
15091562 $ClusterName
15101563 } else {
1511- $ComputerName
1564+ if (
1565+ $ServiceTypeName -ine ' SQL Server' -or
1566+ $ (
1567+ $ServiceProtocol | Where-Object {
1568+ $_.Name -ine ' sm' -and
1569+ $_.IsEnabled -eq $true
1570+ }
1571+ )
1572+ ) {
1573+ # Another protocol besides shared memory is enabled or the service isn't SQL Server\SQL Server Agent; use the FQDN
1574+ $ComputerName
1575+ } else {
1576+ # Shared memory is the only protocol enabled; use the NETBIOS name
1577+ $NetbiosComputerName
1578+ }
15121579 }
15131580 }
15141581 )
15151582 ServiceIpAddress = $ServiceIpAddress
1583+ ServiceProtocols = $ServiceProtocol
15161584 ServiceStartDate = $ServiceStartDate
15171585 ServiceState = $_.ServiceState.ToString ()
15181586 # ServiceType = $_.Type.ToString()
@@ -1663,6 +1731,7 @@ function Find-SqlServerService {
16631731 }
16641732 )
16651733 ServiceIpAddress = $ServiceIpAddress
1734+ ServiceProtocols = $null # TODO: Gather protocol information from registry
16661735 ServiceStartDate = $ServiceStartDate
16671736 ServiceState = $ServiceState
16681737 ServiceTypeName = ' SQL Server'
0 commit comments