Skip to main content

A PowerShell script to test if a TCP or UDP port is open.

<#
    .DESCRIPTION
        A script to test if a TCP or UDP port is open.

    .LINK
        https://github.com/adbertram/Random-PowerShell-Work/blob/master/Networking/Test-NetworkPort.ps1

    .LINK
        https://jonlabelle.com/snippets/view/powershell/test-if-a-network-port-is-open-in-powershell
#>
[CmdletBinding(DefaultParameterSetName = 'TCP')]
# [OutputType([System.Management.Automation.PSCustomObject])]
[OutputType('Bool')]
param (
    [Parameter(Mandatory)]
    [string]$ComputerName,

    [Parameter(Mandatory)]
    [int]$Port,

    [Parameter()]
    [ValidateSet('TCP', 'UDP')]
    [string]$Protocol = 'TCP',

    [Parameter(ParameterSetName = 'TCP')]
    [int]$TcpTimeout = 1000,

    [Parameter(ParameterSetName = 'UDP')]
    [int]$UdpTimeout = 1000
)
process
{
    if ($Protocol -eq 'TCP')
    {
        $TcpClient = New-Object System.Net.Sockets.TcpClient
        $Connect = $TcpClient.BeginConnect($ComputerName, $Port, $null, $null)
        $Wait = $Connect.AsyncWaitHandle.WaitOne($TcpTimeout, $false)

        if (!$Wait)
        {
            $TcpClient.Close()
        }
        else
        {
            $result = $false
            try
            {
                $TcpClient.EndConnect($Connect)
                $result = $true
            }
            catch
            {
                # Write-Warning -Message ('{0}' -f $_.Exception.Message)
                Write-Warning "$($MyInvocation.MyCommand.Name) - '$ComputerName' failed port test on port '$Protocol`:$Port' with error '$($_.Exception.Message)'"
            }
            finally
            {
                $TcpClient.Close()
                $TcpClient.Close()
                $TcpClient.Dispose()
            }
        }
    }
    elseif ($Protocol -eq 'UDP')
    {
        $UdpClient = New-Object System.Net.Sockets.UdpClient
        $UdpClient.Client.ReceiveTimeout = $UdpTimeout
        $UdpClient.Connect($ComputerName, $Port)
        $a = New-Object system.text.asciiencoding
        $byte = $a.GetBytes("$(Get-Date)")
        [void]$UdpClient.Send($byte, $byte.length)
        #IPEndPoint object will allow us to read datagrams sent from any source.
        Write-Verbose "$($MyInvocation.MyCommand.Name) - Creating remote endpoint"
        $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any, 0)

        try
        {
            #Blocks until a message returns on this socket from a remote host.
            Write-Verbose "$($MyInvocation.MyCommand.Name) - Waiting for message return"
            $receivebytes = $UdpClient.Receive([ref]$remoteendpoint)
            [string]$returndata = $a.GetString($receivebytes)
            If ($returndata)
            {
                $result = $true
            }
        }
        catch
        {
            Write-Warning "$($MyInvocation.MyCommand.Name) - '$ComputerName' failed port test on port '$Protocol`:$Port' with error '$($_.Exception.Message)'"
        }
        finally
        {
            $UdpClient.Close()
            $UdpClient.Dispose()
        }
    }

    if ($result)
    {
        $true
    }
    else
    {
        $false
    }
}