петък, юли 15, 2022

Check remote computers for open TCP ports with PowerShell

Fast and simple: 


Test-NetConnection -ComputerName 172.30.33.11 -Port 22

Script:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope Currentuser

cd $env:temp

Start-Transcript -LiteralPath .\Test_results.txt

$ip = Read-Host -Prompt 'Enter server IP'

$p1 = Read-Host -Prompt 'Enter 1st port to test (ENTER or 0 to skip): '

$p2 = Read-Host -Prompt 'Enter 2nd port to test (ENTER or 0 to skip): '

$p3 = Read-Host -Prompt 'Enter 3rd port to test (ENTER or 0 to skip): '

Write-Host ""

Write-Host "Open ports test results:"

if ($p1 -gt 0)

{

if (Test-NetConnection -ComputerName $ip -Port $p1 -InformationLevel Quiet -WarningAction SilentlyContinue) {"Port $p1 is open" } else {"Port $p1 is closed"}

}

else {"No port - no test"}

if ($p2 -gt 0)

{

if (Test-NetConnection -ComputerName $ip -Port $p2 -InformationLevel Quiet -WarningAction SilentlyContinue) {"Port $p2 is open" } else {"Port $p2 is closed"}

}

else {"No port - no test"}


if ($p3 -gt 0)

{

if (Test-NetConnection -ComputerName $ip -Port $p3 -InformationLevel Quiet -WarningAction SilentlyContinue) {"Port $p3 is open" } else {"Port $p3 is closed"}

}

else {"No port - no test"}

Test-NetConnection -ComputerName $ip -TraceRoute -InformationLevel Detailed

Stop-Transcript

Set-ExecutionPolicy -ExecutionPolicy Undefined -Force -Scope CurrentUser


Start-Process notepad .\Test_results.txt



Няма коментари: