Ping的结果转换为布尔值


PowerShell交流中心分类: Powershell基础Ping的结果转换为布尔值
0
LionelZhao asked 8 年 ago

如何在Powershell中把Ping的结果转换为布尔值

1 Answers
1
Best Answer
Mooser Lee 管理员 answered 8 年 ago

低版本的PowerShell,可以这样

PS> $r= & { ping pstips.net | Out-Null;  $? }
PS> $r
True
PS> $r= & { ping no.pstips.net | Out-Null;  $? }
PS> $r
False

高版本的PowerShell,可以这样

PS> Test-Connection pstips.net  -Count 1 -Quiet
True
PS> Test-Connection not.pstips.net  -Count 1 -Quiet
False
LionelZhao replied 8 年 ago

谢谢回复~

铁匠铺的长工 replied 8 年 ago

并不能达到预期效果,只有在请求超时和找不到目标主机时才会报False

铁匠铺的长工 replied 8 年 ago

这个是中文ping ip | out-null ; $?,没发出来,加个中文前缀试试