Powershell测试端口状态

function Test-Port
{
    Param([string]$ComputerName,$port = 5985,$timeout = 1000)
    try
    {
        $tcpclient = New-Object -TypeName system.Net.Sockets.TcpClient
        $iar = $tcpclient.BeginConnect($ComputerName,$port,$null,$null)
        $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
        if(!$wait)
        {
            $tcpclient.Close()
            return $false
        }
        else
        {
            # Close the connection and report the error if there is one
             
            $null = $tcpclient.EndConnect($iar)
            $tcpclient.Close()
            return $true
        }
    }
    catch
    {
        $false
    }
}

 

posted @ 2019-01-18 13:24  JinweiChang  阅读(752)  评论(0编辑  收藏  举报