【问题标题】:Export-csv not writing content in file when inputting info from script running Invoke-Command for Test-NetConnection从运行 Invoke-Command for Test-NetConnection 的脚本输入信息时,Export-csv 未在文件中写入内容
【发布时间】:2020-03-10 01:30:35
【问题描述】:

首先感谢您的阅读。我是 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 输出任何内容。为什么?

【问题讨论】:

  • 去掉-erroraction的,看看它说了什么。

标签: powershell invoke-command pscustomobject


【解决方案1】:

看起来 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 似乎不适用于 Try/Catch 和 SilentlyContinue 作为错误操作,我不确定 catch 无论如何都能正常工作。

再次,我只能进行乏善可陈的测试,不知道您的确切期望是什么,但请尝试其中一些,让我知道结果如何。

【讨论】:

  • 我的主要问题是最后一个脚本没有输出到 CSV。 IP 地址只是一个次要评论。
  • 我试过你的脚本,但仍然没有输出到 CSV,
  • 有没有办法让我的初始脚本比 Try/Catch 更好?
  • 我想我会坚持使用我的原始脚本,只是删除 -Property * 并称之为好。我认为没有其他方法可以更快地做到这一点。
  • 当我测试它时,我确实在 CSV 文件中得到了输出。删除 -Properties 将缩短查询。而且因为你只是在名字之后。你不需要它。也就是说,我的猜测是您的机器数量与速度有关。是否已创建 CSV?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2011-07-04
  • 2019-04-27
  • 2022-01-18
相关资源
最近更新 更多