powershell - Eliminate Inner Loop -
i have 2 loops in program.first loop setting retry peocess , inner loop testing connection status.
for($retry=0;$retry<=3;$retry++) { while (!(test-connection "mycomputer")) { if (time exceed) { $status=$false write-host "machine offline" break } } if($status) { write-host "machine online" break }
}
is there way eliminate inner loop without changing output
not entirely sure mean "time exceeded" - time what?
if want wait between test-connection attempts, can introduce artificial delay start-sleep
:
$computer = "mycomputer" $timeoutseconds = 5 for($retry=0; $retry -lt 3; $retry++) { if(test-connection -computername $computer -count 1 -quiet){ # didn't work write-host "machine offline" # let's wait few seconds before retry start-sleep -seconds $timeoutseconds } else { write-host "machine online!" break } }
the easiest way however, use count
, delay
parameters of test-connection
:
$status = test-connection -computername $computer -count 3 -delay $timeoutseconds
Comments
Post a Comment