问题
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