0

首先感谢您的阅读。我是 Powershell 的新手,所以请原谅我的无知。另外,这是我第一次在这里发帖。

然而,在我拥有它并且它正在工作之前,它非常缓慢。这是初始脚本:

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -Property * -SearchBase "OU=Computers,OU=Domain".Name 

ForEach ($Computer in $RemoteComputers)
{
$result = Invoke-Command -ErrorAction SilentlyContinue -ComputerName $Computer -ScriptBlock {Test-NetConnection -Port 135 server.domain.local}  
[pscustomobject]@{
                    Target = $Computer
                    RemoteAddress = $result.RemoteAddress
                    SourceAddress = (Resolve-Dnsname $Computer -ErrorAction SilentlyContinue).IPAddress
                    Port   = $result.RemotePort
                    Status = $result.tcpTestSucceeded
                }        
}

然后通过这样做来运行它

.\ports.ps1 | Tee-Object -Variable result | Export-Csv ports-all.csv -NoTypeInformation

所以下面我删除了 -Property * 并清理了它。但是 CSV 文件中没有内容。谁能告诉我为什么当我运行它时 csv 文件中没有内容?

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -SearchBase "OU=Computers,OU=Domain").Name 

$results = try {
    Invoke-Command -ErrorAction Stop -ComputerName $RemoteComputers -ScriptBlock {
        $Test = Test-NetConnection -Port 135 "server.domain.local"
        [pscustomobject]@{
            Target = $Env:COMPUTERNAME
            RemoteAddress = $Test.RemoteAddress
            SourceAddress = (Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress
            Port   = $Test.RemotePort
            Status = $Test.tcpTestSucceeded
        }    
    }
}
catch {
    Write-Output ([pscustomobject]@{
        Target = $_.TargetObject
        RemoteAddress = $null
        SourceAddress = $null
        Port   = $null
        Status = "Failed to connect"
    })
}

$results | select target, remoteaddress, sourceaddress, port, status | export-csv file.csv

所以最后一个脚本不会向 CSV 输出任何内容。为什么?

4

1 回答 1

0

看起来 Resolve-DnsName 将返回 IPv6 和 v4 地址的集合。我想这在某种程度上取决于您的 DNS 区域中的内容。我不知道它是否可靠,但您可以将命令包装在数组子表达式中,然后引用索引 [1]

SourceAddress = @((Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress)[1]

我的测试不是真实世界的,但看起来您可能想将 try catch 放在Invoke-CommandScriptBlock 的内部。

也许是这样的:

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -SearchBase "OU=Computers,OU=Domain").Name 

$results = 
    Invoke-Command -ErrorAction Stop -ComputerName $RemoteComputers -ScriptBlock {
        try {
        $Test = Test-NetConnection -Port 135 "server.local.com"
        [pscustomobject]@{
            Target = $Env:COMPUTERNAME
            RemoteAddress = $Test.RemoteAddress
            SourceAddress = @((Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress)[1]
            Port   = $Test.RemotePort
            Status = $Test.tcpTestSucceeded
        } }
        catch {
        [pscustomobject]@{
            Target = $_.TargetObject
            RemoteAddress = $null
            SourceAddress = $null
            Port   = $null
            Status = "Failed to connect"
        } }
}

$results | select target, remoteaddress, sourceaddress, port, status | export-csv file.csv

我还要指出,Test-NetConnectionTry/Catch 似乎不能很好地工作,并且SilentlyContinue作为错误操作,我不确定 catch 无论如何都能正常工作。

再说一次,我只能做乏味的测试,不知道你的确切期望是什么,但试试这个,让我知道它是怎么回事。

于 2020-03-10T02:03:48.570 回答