mdriscoll93

test-netconnect-1liner.ps1

Feb 13th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 0.84 KB | Software | 0 0
  1. # Test each port individually against any external host
  2. # note: script will also accept `.\test-netconnect -ComputerName <IPv4_addr>`
  3. # if you choose to execute as if it's just Test-NetConnection
  4. param (
  5.     [Parameter(Mandatory=$true, HelpMessage="Enter the target IPv4 address")]
  6.     [string]$ComputerName = (Read-Host "Please enter the target IPv4 address")
  7. )
  8.  
  9. # Test each port individually against any external host
  10. Test-NetConnection w3.org -Port 443
  11.  
  12. # Quick one-liner to test multiple ports against the provided external host
  13. $ports = 8000,8001,8002,22,53,80,443,5000,5001,5002
  14. $ports | ForEach-Object {
  15.     $port = $_;
  16.     if (Test-NetConnection -ComputerName $ComputerName -Port $port -InformationLevel Quiet -WarningAction SilentlyContinue) {
  17.         "Port $port is open"
  18.     } else {
  19.         "Port $port is closed"
  20.     }
  21. }
Add Comment
Please, Sign In to add comment