Test network port RRS feed

  • Question

  • I have used the following code to determine if a port is open on a server.  It seems the same error is returned whether the port is not opened on a server or is being blocked by a network firewall.  Is there a way to determine if the connection is refused because the port is not open, or it is being blocked by a network firewall?(not Windows firewall)

    Try {
    $test = New-Object System.Net.Sockets.TcpClient("$server","$port")
      If ($test.Connected -eq $true)
        Write-Host "The $server is listening on $port"
    Catch {
    if ($error[0].Categoryinfo.Reason -eq "MethodInvocationException")
     Write-Host "$server refused the connection"

    • Moved by Bill_Stewart Monday, July 29, 2019 7:32 PM Abandoned
    Thursday, October 4, 2018 4:40 PM

All replies