Skip to content

Commit 5d49814

Browse files
authored
Update Test-SCOMPorts.ps1
1 parent d63d631 commit 5d49814

File tree

1 file changed

+158
-73
lines changed

1 file changed

+158
-73
lines changed

Powershell/Test-SCOMPorts.ps1

Lines changed: 158 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,190 @@
1-
<#
1+
<#
22
.SYNOPSIS
33
Test the ports SCOM Uses with Test-NetConnection Automatically.
44
55
.DESCRIPTION
66
This script tests the ports for SCOM.
77
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+
820
.PARAMETER Servers
921
An array of Servers, or alternatively you can pipe in objects from Get-SCOMAgent or Get-SCOMManagementServer.
1022
1123
.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
1527
1628
.NOTES
1729
.AUTHOR
18-
Blake Drumm (v-bldrum@microsoft.com)
30+
Blake Drumm (blakedrumm@microsoft.com)
1931
.LAST MODIFIED
20-
06/10/2021
21-
32+
07/29/2021
33+
2234
https://www.stefanroth.net/2013/10/08/powershell-4-0-checking-scom-required-ports/
2335
#>
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
5837
{
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)
6054
{
61-
Write-Host -ForegroundColor Yellow "Checking SCOM ports on computer: $env:COMPUTERNAME"
62-
63-
64-
ForEach ($port in $ports.GetEnumerator())
55+
if (!$OutputType)
6556
{
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'
7858
}
7959
}
8060
else
8161
{
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+
{
88123
ForEach ($port in $ports.GetEnumerator())
89124
{
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))
95129
{
130+
True { $payload += new-object psobject -property @{ Availability = 'Up'; 'Service Name' = $($port.Name); Port = $($port.Value); SourceServer = $env:COMPUTERNAME; DestinationServer = $server } }
96131

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 } }
100133
}
101-
102134
}
135+
103136
}
137+
Write-Host '> Complete!' -ForegroundColor Green
138+
return $payload
104139
}
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
105190
}

0 commit comments

Comments
 (0)