How to check open ports using powershell? -
i write script check radom ip or hostname see if ports open. here have far. scripts name checkports.
foreach ($xhost in $computername){ write-host $xhost foreach ($port in $ports) { $socket = new-object system.net.sockets.tcpclient $connection = $socket.beginconnect($xhost,$port,$null,$null) $connection.asyncwaithandle.waitone(5000,$false) | out-null if ($connection -eq $true) { write-host = "$xhost port $port open" } else { write-host = "port $port closed" } $socket.endconnect($connection) $socket.close() } }
i input values in following way: .\checkport '192.186.1.5' or '192.168.1.5', '192.168.1.105', 192.168.1.110' | checkport
it doesn't seem reading ip address or displaying results.
i wondering if point out there show me doing wrong in script?
i've been able use 'test-port' function boe prox similar scan/ reporting functions, code available on poshcode:
when needed test ports directory health, built csv 'port' , 'protocol' columns, added port number/ protocol each port check. used in following script:
. .\test-port.ps1 $computerstocheck = get-content .\computers.txt $portlist = import-csv .\portlist.csv foreach($entry in $portlist) { $testportparams = @{ port = $($entry.port) } if( $($entry.protocol) -eq "tcp") { $testportparams += @{ tcp = $true } } else { $testportparams += @{ udp = $true } } $outlog = "porttest-$($entry.port)-$($entry.protocol).txt" $computerstocheck | test-port @testportparams | sort-object -property open,name -descending | format-table -auto -outvariable status add-content -path $outlog -value $status }
you build feeder script build range of ip addresses , ports scan.
Comments
Post a Comment