在 PowerShell 中使用 Test-Connection 时如何 return 布尔值和 TestConnectionCommand+PingStatus?
How do I return a boolean value and TestConnectionCommand+PingStatus when using Test-Connection in PowerShell?
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip
此代码 return TestConnectionCommand+PingStatus while,
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip -Quiet
此代码 return 是一个布尔值
问题是,我如何 return TestConnectionCommand+PingStatus 和布尔值只发送一次 ping。
如果在没有 -Quiet 的情况下无法 ping,该命令不会 return 任何内容。然后查看状态。
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip -ErrorAction SilentlyContinue
if ($success -and ($success.Status -eq "Success")) {
# do something with the $success
}
else {
# connection failed
}
$ping = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip
for ($p = 0; $p -lt $ping.length; $p++) {
if($ping[$p].Status -eq "Success"){
Write-Host -ForegroundColor Green $ping[$p].Status
$result = "True"
}else{
Write-Host -ForegroundColor Red $p.Status
$result = "False"
break
}
}
$result | Out-File -LiteralPath $store -Append
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip
此代码 return TestConnectionCommand+PingStatus while,
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip -Quiet
此代码 return 是一个布尔值
问题是,我如何 return TestConnectionCommand+PingStatus 和布尔值只发送一次 ping。
如果在没有 -Quiet 的情况下无法 ping,该命令不会 return 任何内容。然后查看状态。
$success = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip -ErrorAction SilentlyContinue
if ($success -and ($success.Status -eq "Success")) {
# do something with the $success
}
else {
# connection failed
}
$ping = Test-Connection -Ping -IPV4 -Count 4 -TimeoutSeconds 3 -TargetName $ip
for ($p = 0; $p -lt $ping.length; $p++) {
if($ping[$p].Status -eq "Success"){
Write-Host -ForegroundColor Green $ping[$p].Status
$result = "True"
}else{
Write-Host -ForegroundColor Red $p.Status
$result = "False"
break
}
}
$result | Out-File -LiteralPath $store -Append