Is there a way to treat Ping-Host as a boolean in PowerShell?

落爺英雄遲暮 提交于 2019-12-08 05:20:58

问题


I'd like to do something like this:

if (Ping-Host server1) { blah }

Anyone know of a simple way?


回答1:


Something like this?

if ($(Ping-Host server1 -count 1 -timeout 10).received -eq 1) { blah }



回答2:


If you are on PowerShell 2.0 I would recommend using Test-Connection -Quiet. It returns a simple true or false indicating whether or not the host can be reached via ping. BTW one of the benefits of Test-Connection is it's built-in whereas ping-host isn't.



来源:https://stackoverflow.com/questions/1595298/is-there-a-way-to-treat-ping-host-as-a-boolean-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!