【问题标题】:Unable to pipe names of pinged computers that return false to test-netconnection无法通过管道将返回 false 的 ping 计算机的名称传递给 test-netconnection
【发布时间】:2020-08-31 17:07:03
【问题描述】:

我正在尝试创建一个基本脚本,该脚本从文本文件中提取计算机名称列表,然后对它们执行 ping 操作,并返回 true 或 false。然后我想将返回 false 的那些输出到一个文本文件,这样我就可以知道哪些没有响应。

最接近我想要的结果如下:

$workstations = Get-Content "workstation_list.txt"
$workstations | Test-NetConnection -InformationLevel Quiet -WarningAction SilentlyContinue

但是,每当我尝试将结果传递到任何地方时,我得到的都是真假。

如何传递 $workstations 数组中的原始名称以显示所有返回 false 的名称?

我试过了:

$workstations = Get-Content "workstation_list.txt"
$workstations | 
    Test-NetConnection -InformationLevel Detailed -WarningAction SilentlyContinue | 
        Select-Object computername, pingsucceeded | 
            if(pingsucceeded -eq False){write-output} else{continue}

出现以下错误:

pingsucceeded : The term 'pingsucceeded' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:11 char:144
+ ...  Select-Object computername, pingsucceeded | if(pingsucceeded -eq Fal ...
+                                                     ~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (pingsucceeded:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException*

但是我不知道如何只返回在我 ping 时返回 false 的计算机的原始名称。

然后我想将它输出到一个文本文件,但是如果我不能让它将正确的信息传递到屏幕它也不会转到一个文件。

我是接近还是需要以完全不同的方式处理这个问题?

谢谢!

PS.这是我第一次发布有关堆栈溢出的问题,如果我需要以不同的方式提供信息以使您更容易回答,请提供建设性的反馈,以便我将来做得更好。

【问题讨论】:

    标签: powershell pipe ping powershell-5.0


    【解决方案1】:

    我建议使用PSCustomObject 来存储您的结果,如下所示:

    $workstations = Get-Content "workstation_list.txt"
    $Result =
    foreach ($ComputerName in $workstations) {
        [PSCustomObject]@{
            ComputerName = $ComputerName
            Online = (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)
        }
    }
    $Result
    

    这样,如果需要,您可以使用变量$Result 进行进一步的步骤。输出成功的例子

    $Result | Where-Object -Property 'Online' -EQ -Value $true
    

    或者过滤不成功的输出到另一个文件例如:

    $Result | 
        Where-Object -Property 'Online' -EQ -Value $false |
            Select-Object -ExpandProperty ComputerName |
                Out-File -FilePath 'offline_workstation_list.txt'
    

    【讨论】:

    • 谢谢你这对我有用!有没有一种简单的方法可以让它输出从第一个文本文件中提取的原始计算机名称?还是我只需要添加到管道并使用 dnslookup 或类似的东西?
    • 嗯...我不确定我是否理解这个问题。计算机名来自文本文件。
    【解决方案2】:

    您需要学习一些基本的 powershell。对于一件事,您不能通过管道传递到 if 语句,但可以传递到 foreach-object:

    $workstations = Get-Content "workstation_list.txt"
    $workstations |
    Test-NetConnection -InformationLevel Detailed -WarningAction SilentlyContinue |
    Select-Object computername, pingsucceeded |
    foreach-object { if($_.pingsucceeded -eq $False){write-output $_} else{continue} }
    
    ComputerName  PingSucceeded
    ------------  -------------
    microsoft.com         False
    

    【讨论】:

    • 我在使用 foreach-object 时遇到的问题是,当我尝试进一步管道输出时,它总是说“不允许空管道元素”。有没有办法解决这个问题?我尝试将上述管道传输到 | Out-File -FilePath 'offline_workstation_list.txt' 并没有成功。
    • @JakeTheWandererSkolez 你不能从 if 管道,但你可以从 foreach-object 管道。
    • 谢谢!我一直很困惑那个。我没有意识到这是 if 造成的。
    • @JakeTheWandererSkolez 你也不能从其他形式的 foreach 管道foreach ($i in 1..3) { $i } 。这会让人失望。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2018-10-07
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多