|
1 | | - <# |
| 1 | +<# |
2 | 2 | .SYNOPSIS |
3 | 3 | Test the ports SCOM Uses with Test-NetConnection Automatically. |
4 | 4 | |
5 | 5 | .DESCRIPTION |
6 | 6 | This script tests the ports for SCOM. |
7 | 7 | |
| 8 | + .PARAMETER SourceServer |
| 9 | + A description of the SourceServer parameter. |
| 10 | + |
| 11 | + .PARAMETER DestinationServer |
| 12 | + A description of the DestinationServer parameter. |
| 13 | + |
| 14 | + .PARAMETER OutputFile |
| 15 | + A description of the OutputFile parameter. |
| 16 | + |
| 17 | + .PARAMETER OutputType |
| 18 | + A description of the OutputType parameter. |
| 19 | + |
8 | 20 | .PARAMETER Servers |
9 | 21 | An array of Servers, or alternatively you can pipe in objects from Get-SCOMAgent or Get-SCOMManagementServer. |
10 | 22 | |
11 | 23 | .EXAMPLE |
12 | | - PS C:\> Get-SCOMAgent | Where {$_.Name -match "IIS-server"} | .\Test-SCOMPorts |
13 | | - PS C:\> Get-SCOMManagementServer | .\Test-SCOMPorts |
14 | | - PS C:\> .\Test-SCOMPorts -Servers Agent1.contoso.com, SQL-Server.contoso.com |
| 24 | + PS C:\> Get-SCOMAgent | Where {$_.Name -match "IIS-server"} | .\Test-SCOMPorts |
| 25 | + PS C:\> Get-SCOMManagementServer | .\Test-SCOMPorts |
| 26 | + PS C:\> .\Test-SCOMPorts -Servers Agent1.contoso.com, SQL-Server.contoso.com |
15 | 27 | |
16 | 28 | .NOTES |
17 | 29 | .AUTHOR |
18 | | - Blake Drumm (v-bldrum@microsoft.com) |
| 30 | + Blake Drumm (blakedrumm@microsoft.com) |
19 | 31 | .LAST MODIFIED |
20 | | - 06/10/2021 |
21 | | - |
| 32 | + 07/29/2021 |
| 33 | + |
22 | 34 | https://www.stefanroth.net/2013/10/08/powershell-4-0-checking-scom-required-ports/ |
23 | 35 | #> |
24 | | -param |
25 | | -( |
26 | | - [Parameter(Mandatory = $false, |
27 | | - ValueFromPipeline = $true, |
28 | | - Position = 1)] |
29 | | - [array]$Servers |
30 | | -) |
31 | | -$ports = @{ |
32 | | - "Management Server / Agent Port" = 5723; |
33 | | - "Console Port" = 5724; |
34 | | - "Connector Framework Source Port" = 51905; |
35 | | - "ACS Forwarder Port" = 51909; |
36 | | - "AEM Port" = 51906; |
37 | | - "SQL Server (Default) Port" = 1433; |
38 | | - "SSH Port" = 22; |
39 | | - "WS-MAN Port" = 1270; |
40 | | - "Web Console (HTTP) Port" = 80; |
41 | | - "Web Console (HTTPS) Port" = 443; |
42 | | - "SNMP (Get) Port" = 161; |
43 | | - "SNMP (Trap) Port" = 162 |
44 | | -} |
45 | | -if (!$Servers) |
46 | | -{ |
47 | | - $servers = $env:COMPUTERNAME |
48 | | -} |
49 | | -elseif ($Servers -match 'Microsoft.EnterpriseManagement.Administration.ManagementServer') |
50 | | -{ |
51 | | - $Servers = $Servers.DisplayName |
52 | | -} |
53 | | -elseif ($Servers -match 'Microsoft.EnterpriseManagement.Administration.AgentManagedComputer') |
54 | | -{ |
55 | | - $Servers = $Servers.DisplayName |
56 | | -} |
57 | | -foreach ($server in $Servers) |
| 36 | +function Test-SCOMPorts |
58 | 37 | { |
59 | | - if ($server -match $env:COMPUTERNAME) |
| 38 | + param |
| 39 | + ( |
| 40 | + [Parameter(Mandatory = $false, |
| 41 | + Position = 1)] |
| 42 | + [array]$SourceServer, |
| 43 | + [Parameter(Mandatory = $true, |
| 44 | + ValueFromPipeline = $true, |
| 45 | + Position = 2)] |
| 46 | + [array]$DestinationServer, |
| 47 | + [Parameter(Position = 3)] |
| 48 | + [string]$OutputFile, |
| 49 | + [Parameter(Position = 4)] |
| 50 | + [ValidateSet("Text", "CSV", "Table")] |
| 51 | + [string[]]$OutputType = 'Table' |
| 52 | + ) |
| 53 | + if ($OutputFile) |
60 | 54 | { |
61 | | - Write-Host -ForegroundColor Yellow "Checking SCOM ports on computer: $env:COMPUTERNAME" |
62 | | - |
63 | | - |
64 | | - ForEach ($port in $ports.GetEnumerator()) |
| 55 | + if (!$OutputType) |
65 | 56 | { |
66 | | - |
67 | | - |
68 | | - $nettest = Test-NetConnection -Computername $server -Port $port.Value -WarningAction SilentlyContinue |
69 | | - |
70 | | - Switch ($($nettest.TcpTestSucceeded)) |
71 | | - { |
72 | | - |
73 | | - True { Write-Host -Foregroundcolor Green "AVAILABLE - $($port.Name): $($port.Value)" } |
74 | | - |
75 | | - False { Write-Host -ForegroundColor Red "FAILED - $($port.Name): $($port.Value)" } |
76 | | - } |
77 | | - |
| 57 | + $OutputType = 'Text' |
78 | 58 | } |
79 | 59 | } |
80 | 60 | else |
81 | 61 | { |
82 | | - Invoke-Command -ComputerName $server -ScriptBlock { |
83 | | - Write-Host -ForegroundColor Yellow "Checking SCOM ports on computer: $env:COMPUTERNAME" |
84 | | - |
85 | | - $ports = $using:ports |
86 | | - |
87 | | - |
| 62 | + if ($OutputType -eq 'Text') |
| 63 | + { |
| 64 | + $OutputFile = "$PSScriptRoot`\SCOM-Port-Checker.txt" |
| 65 | + } |
| 66 | + elseif ($OutputType -eq 'CSV') |
| 67 | + { |
| 68 | + $OutputFile = "$PSScriptRoot`\SCOM-Port-Checker.csv" |
| 69 | + } |
| 70 | + } |
| 71 | + if (!$SourceServer) |
| 72 | + { |
| 73 | + $SourceServer = $env:COMPUTERNAME |
| 74 | + } |
| 75 | + elseif ($SourceServer -match 'Microsoft.EnterpriseManagement.Administration.ManagementServer') |
| 76 | + { |
| 77 | + $SourceServer = $SourceServer.DisplayName |
| 78 | + } |
| 79 | + elseif ($SourceServer -match 'Microsoft.EnterpriseManagement.Administration.AgentManagedComputer') |
| 80 | + { |
| 81 | + $SourceServer = $SourceServer.DisplayName |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + $SourceServer = $SourceServer |
| 86 | + } |
| 87 | + Write-Output " " |
| 88 | + Write-Output @" |
| 89 | +================================ |
| 90 | +Starting SCOM Port Checker |
| 91 | +"@ |
| 92 | + function Check-SCOMPorts |
| 93 | + { |
| 94 | + param |
| 95 | + ( |
| 96 | + [Parameter(Mandatory = $true, |
| 97 | + Position = 0)] |
| 98 | + [array]$DestinationServer, |
| 99 | + [Parameter(Mandatory = $false, |
| 100 | + Position = 1)] |
| 101 | + [array]$SourceServer |
| 102 | + ) |
| 103 | + $payload = $null |
| 104 | + $payload = @() |
| 105 | + Write-Host " Running from: " -NoNewLine |
| 106 | + Write-Host $env:COMPUTERNAME -ForegroundColor Cyan -NoNewLine |
| 107 | + $ports = @{ |
| 108 | + "Management Server / Agent Port" = 5723; |
| 109 | + "Console Port" = 5724; |
| 110 | + "Connector Framework Source Port" = 51905; |
| 111 | + "ACS Forwarder Port" = 51909; |
| 112 | + "AEM Port" = 51906; |
| 113 | + "SQL Server (Default) Port" = 1433; |
| 114 | + "SSH Port" = 22; |
| 115 | + "WS-MAN Port" = 1270; |
| 116 | + "Web Console (HTTP) Port" = 80; |
| 117 | + "Web Console (HTTPS) Port" = 443; |
| 118 | + "SNMP (Get) Port" = 161; |
| 119 | + "SNMP (Trap) Port" = 162 |
| 120 | + } |
| 121 | + foreach ($server in $DestinationServer) |
| 122 | + { |
88 | 123 | ForEach ($port in $ports.GetEnumerator()) |
89 | 124 | { |
90 | | - |
91 | | - |
92 | | - $nettest = Test-NetConnection -Computername $server -Port $port.Value -WarningAction SilentlyContinue |
93 | | - |
94 | | - Switch ($($nettest.TcpTestSucceeded)) |
| 125 | + $tcp = $null |
| 126 | + $tcp = Test-NetConnection -Computername $server -Port $port.Value -WarningAction SilentlyContinue |
| 127 | + Write-Host '-' -ForegroundColor Green -NoNewline |
| 128 | + Switch ($($tcp.TcpTestSucceeded)) |
95 | 129 | { |
| 130 | + True { $payload += new-object psobject -property @{ Availability = 'Up'; 'Service Name' = $($port.Name); Port = $($port.Value); SourceServer = $env:COMPUTERNAME; DestinationServer = $server } } |
96 | 131 |
|
97 | | - True { Write-Host -Foregroundcolor Green "AVAILABLE - $($port.Name): $($port.Value)" } |
98 | | - |
99 | | - False { Write-Host -ForegroundColor Red "FAILED - $($port.Name): $($port.Value)" } |
| 132 | + False { $payload += new-object psobject -property @{ Availability = 'Down'; 'Service Name' = $($port.Name); Port = $($port.Value); SourceServer = $env:COMPUTERNAME; DestinationServer = $server } } |
100 | 133 | } |
101 | | - |
102 | 134 | } |
| 135 | + |
103 | 136 | } |
| 137 | + Write-Host '> Complete!' -ForegroundColor Green |
| 138 | + return $payload |
104 | 139 | } |
| 140 | + $sb = (get-item Function:Check-SCOMPorts).ScriptBlock |
| 141 | + foreach ($source in $SourceServer) |
| 142 | + { |
| 143 | + if ($source -match $env:COMPUTERNAME) |
| 144 | + { |
| 145 | + $scriptout += Check-SCOMPorts -SourceServer $source -DestinationServer $DestinationServer |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + $scriptout += Invoke-Command -ComputerName $source -ScriptBlock $sb -ArgumentList ( ,$DestinationServer) |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + $finalout = $scriptout | select 'Service Name', SourceServer, Port, Availability, DestinationServer | Sort-Object -Property @{ |
| 155 | + expression = 'SourceServer' |
| 156 | + descending = $false |
| 157 | + }, @{ |
| 158 | + expression = 'DestinationServer' |
| 159 | + descending = $false |
| 160 | + }, @{ |
| 161 | + expression = 'Port' |
| 162 | + descending = $false |
| 163 | + } |
| 164 | + if ($OutputType -eq 'CSV') |
| 165 | + { |
| 166 | + Write-Host "Output to " -NoNewline -ForegroundColor Gray |
| 167 | + Write-Host $OutputFile -NoNewline -ForegroundColor Cyan |
| 168 | + $finalout | Export-Csv -Path $OutputFile -NoTypeInformation |
| 169 | + } |
| 170 | + elseif ($OutputType -eq 'Text') |
| 171 | + { |
| 172 | + Write-Host "Output to " -NoNewline -ForegroundColor Gray |
| 173 | + Write-Host $OutputFile -NoNewline -ForegroundColor Cyan |
| 174 | + $finalout | ft * | Out-File $OutputFile |
| 175 | + } |
| 176 | + elseif ($OutputType -eq 'Table') |
| 177 | + { |
| 178 | + $finalout | ft * |
| 179 | + } |
| 180 | +} |
| 181 | +if ($SourceServer -or $DestinationServer -or $OutputFile -or $OutputType) |
| 182 | +{ |
| 183 | + Test-SCOMPorts -SourceServer $SourceServer -DestinationServer $DestinationServer -OutputFile $OutputFile -OutputType $OutputType |
| 184 | +} |
| 185 | +else |
| 186 | +{ |
| 187 | + #Enter the Server you want to check ports against here. |
| 188 | + # ex. Test-SCOMPorts -DestinationServer 'Agent1.contoso.com' |
| 189 | + Test-SCOMPorts |
105 | 190 | } |
0 commit comments