Powershell try/catch with test-connection
try
is for catch
ing exceptions. You're using the -Quiet
switch so Test-Connection
returns $true
or $false
, and doesn't throw
an exception when the connection fails.
Just do:
if (Test-Connection -computername $computer -Quiet -Count 1) {
# succeeded do stuff
} else {
# failed, log or whatever
}