从运行Test-NetConnection的Invoke-Command的脚本输入信息时,Export-csv不在文件中写入内容

问题描述 投票:0回答:1

首先感谢您的阅读。我是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。为什么?

powershell invoke-command pscustomobject
1个回答
0
投票

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

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

我的测试不是真实世界,但是您似乎可能想将try catch放在Invoke-Command的ScriptBlock内部。

也许像:

$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-NetConnection似乎不适用于“尝试/捕获”,并且SilentlyContinue作为错误操作,我不确定捕获是否可以正常运行。

同样,我只能进行乏味的测试,不知道您的确切期望是什么,但是尝试其中的一些并让我知道它的运行情况。

© www.soinside.com 2019 - 2024. All rights reserved.