Created
May 6, 2022 18:20
-
-
Save karoltheguy/74aa26043528a355270d25158574de4f to your computer and use it in GitHub Desktop.
Remote Port Query by Powershell
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
| #$RemoteServer = Read-Host "Please provide destination to test" | |
| $DestinationHost = "" | |
| $SourceServers = "" #for multi servers: "server1", "server2", "server3" | |
| $PortList = "443" #for multi ports: "80, 443, 566" | |
| $PortType = "TCP" | |
| $localportqry = "`"E:\RPA Tools\PortQryUI\PortQry.exe`"" | |
| $PortArray = @() | |
| if ($PortType -eq "UDP") { $PortCmd = "-p UDP" } Else { $PortCmd = "-p TCP" } | |
| Foreach ($Server in $SourceServers){ | |
| Write-Host "Checking $Server" -ForegroundColor Yellow | |
| $Array = @() | |
| $PortQuery = $Ports = $Object = $PortObject = $Null | |
| $PortQuery = Invoke-Command $Server -ScriptBlock{cmd.exe /c "$using:localportqry -n $using:DestinationHost -o $using:PortList $using:PortCmd"} | |
| #Extract Result lines | |
| $Ports = $PortQuery | Where-Object {$_.StartsWith("$PortType port")} | |
| Foreach ($Line in $Ports){ | |
| $PortNumber = ($Line -split "$PortType port ")[1].Trim() | |
| $PortNumber = ($PortNumber -split " ")[0].Trim() | |
| $Status = ($Line -split ": ")[1].Trim() | |
| $Object = New-Object PSObject -Property ([ordered]@{ | |
| PortNumber = $PortNumber | |
| Status = $Status | |
| }) | |
| # Add custom object to our array | |
| $Array += $Object | |
| } | |
| If($Array){ | |
| $PortObject = New-Object PSCustomObject | |
| $PortObject | Add-Member -MemberType NoteProperty -Name "Server" -Value $Server | |
| Foreach($item in $Array){ | |
| $PortNr = $Null | |
| $PortNr = $Item.portnumber | |
| $PortObject | Add-Member -MemberType NoteProperty -Name $PortNr -Value $item.status | |
| } | |
| $PortArray += $PortObject | |
| } | |
| } | |
| $PortArray | Format-Table -AutoSize -Wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment