create function to test port on specific i.p to find out if its listen and can accept connection .
for this method we will use with “New-Object System.Net.Sockets.TcpClient”
we’ll add it to a variable $Ping
1 |
$Ping = New-Object System.Net.Sockets.TcpClient |
so we can tell the object which IP and Port we would like to test with it ,as this object has this members options
view all the members it has
C:\PS> $Ping | Get-Member
now lets create it in function so we can use it in a sec’ ,and make it with colors for success/false indications
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function Test-Socket { [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$True)] $IP, $Port ) $Ping = New-Object System.Net.Sockets.TcpClient try { $Ping.Connect("$IP", $port) if ($Ping.Connected) {Write-host "$Port on $IP is O.K!" -BackgroundColor Black -ForegroundColor Green} } catch { write-host "Notice!!! $Port on $IP is Not Available!" -BackgroundColor Black -ForegroundColor Red} } |
(Visited 851 times, 1 visits today)