Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法将返回false的ping计算机的名称管道化以测试netconnection_Powershell_Pipe_Ping_Powershell 5.0 - Fatal编程技术网

Powershell 无法将返回false的ping计算机的名称管道化以测试netconnection

Powershell 无法将返回false的ping计算机的名称管道化以测试netconnection,powershell,pipe,ping,powershell-5.0,Powershell,Pipe,Ping,Powershell 5.0,我试图创建一个基本脚本,从文本文件中提取计算机名列表,然后ping它们,并返回true或false。然后,我想将返回false的内容输出到一个文本文件中,以便知道哪些内容没有响应 我最接近我想要的是: $workstations = Get-Content "workstation_list.txt" $workstations | Test-NetConnection -InformationLevel Quiet -WarningAction SilentlyContinue 然而,每当我

我试图创建一个基本脚本,从文本文件中提取计算机名列表,然后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.这是我第一次发布关于堆栈溢出的问题,如果我需要以不同的方式提供信息,让您更容易回答,请提供建设性的反馈,以便我将来能做得更好。

我建议使用
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'

我建议使用
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'

您需要学习一些基本的powershell。一方面,不能通过管道连接到if语句,但可以连接到foreach对象:

$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

您需要学习一些基本的powershell。一方面,不能通过管道连接到if语句,但可以连接到foreach对象:

$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对象时遇到的问题是,当我尝试进一步管道化其输出时,它总是说“不允许使用空管道元素”。有没有办法绕过这个问题?我尝试将上述内容传输到| Out File-FilePath“offline_workstation_list.txt”,但没有成功。@jakethewalterskolez您不能从if进行传输,但可以从foreach-object进行传输。谢谢!我在那件事上被难住了。我不知道是if造成的。@jakethewalterskolez你也不能从另一种形式的foreach
foreach($I in 1..3){$I}
。我在使用foreach对象时遇到的问题是,当我尝试进一步管道化其输出时,它总是说“不允许使用空管道元素”。有没有办法绕过这个问题?我尝试将上述内容传输到| Out File-FilePath“offline_workstation_list.txt”,但没有成功。@jakethewalterskolez您不能从if进行传输,但可以从foreach-object进行传输。谢谢!我在那件事上被难住了。我不知道是if造成的。@jakethewalterskolez你也不能从另一种形式的foreach
foreach($I in 1..3){$I}
。这让人很惊讶。谢谢你,这对我很有用!有没有一种简单的方法可以让它输出从第一个文本文件中提取的原始计算机名?或者我只需要添加到管道中并使用dnslookup或类似的东西吗?嗯。。。我不确定我是否理解这个问题。计算机名来自文本文件。谢谢你,这对我有用!有没有一种简单的方法可以让它输出从第一个文本文件中提取的原始计算机名?或者我只需要添加到管道中并使用dnslookup或类似的东西吗?嗯。。。我不确定我是否理解这个问题。计算机名来自文本文件。