Would you like to know what ports are openĀ on a host? With the introduction of Powershell 4.0 there’s a new cmdlet called Test-Netconnection which in it’s simples form basically is ping
. It does, however, have some more advanced features, like scanning towards ports on a host.
Here’s a quick and dirty script to scan a single host for single port, or a sequential range of ports.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
################################### # Reidar J. Boldevin - 22.05.2014 # # The Tech Depot # # http://www.boldevin.com # ################################### # Collect human error $target = Read-Host "What is the target name or IP?" $startport = Read-Host "What is the starting port?" $endport = Read-Host "What is the ending port?" # Translate human error to working script variables $range = $startport..$endport $result =@() # Test each port foreach ($port in $range) { $result += Test-NetConnection -ComputerName $target -Port $port } # Format output $format = @{Expression={$_.RemotePort};Label="Remote Port";width=13}, @{Expression={$_.TcpTestSucceeded};Label="Port Open";width=11}, @{Expression={$_.PingSucceeded};Label="Target available";width=18} # Show the code monkey the fruits of his labor $result | ft $format |
I have to admit that I miss the -Source switch in “Test-Connection” that allows you to choose where the connection request should originate, but perhaps we’ll see that in Powershell/WMF 5.0?