Powershell检查所有电脑的远程端口


结合先前学习的技能,我们可以使用Test-Port和微软AD模块,来对公司电脑做一次端口检查,可以获取哪些电脑的Powershell端口有打开:

#requires -Version 1 -Modules ActiveDirectory
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 
    }
}


Get-ADComputer -Filter * | 
Select-Object -ExpandProperty dnsHostName |
ForEach-Object {
    Write-Progress -Activity 'Testing Port' -Status $_
} |
Where-Object -FilterScript {
    Test-Port -ComputerName $_ 
}

原文地址: Finding Computers with PowerShell Remoting

本文链接: https://www.pstips.net/finding-computers-with-powershell-remoting.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注