无法通过管道传递将false返回到测试网络连接的ping通计算机的名称

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

我正在尝试创建一个基本脚本,该脚本从文本文件中提取计算机名称列表,然后对其进行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。这是我第一次发布有关堆栈溢出的问题,如果我需要以其他方式提供信息以使您更容易回答,请提供建设性的反馈意见,以便将来我做得更好。] >

我正在尝试创建一个基本脚本,该脚本从文本文件中提取计算机名称列表,然后对其进行ping操作,并返回true或false。然后,我想将返回false的内容输出到文本文件中...

powershell pipe ping powershell-5.0
2个回答
0
投票

我建议使用PSCustomObject这样存储您的结果:


1
投票

您需要学习一些基本的功能。您无法通过管道将if语句传递给一件事,但是可以传递给foreach-object:

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