Author Posts

January 1, 2012 at 12:00 am

by Zushakon at 2012-09-04 22:56:33

Hello,

I would like to ask a rather simple question... how do I skip one loop meaning if I check some servers for some services using loop foreach i would like to skip a loop if server is off.

by lumm71 at 2012-09-05 00:18:27

You can use "continue" to end one loop:

while ($a -lt 10)
{
$a += 1
if ($a -eq 5){continue}
Write-Host $a
}

it writes all numbers from 1 to 10 except 5 to the screen.

by Techibee.Author at 2012-09-05 03:00:42

Is this something you are looking for?

foreach ($a in ("a","b","c","d")) {
write-host ""
Write-Host $a
foreach($b in 1,2,3,4) {
if($b -eq 3) {
Write-host "Skipping $b"
Continue
Write-Host "This will not appear"
} else {
Write-host $b
}
}
}

by mjolinor at 2012-09-05 04:08:58

A simple if statement can work:
foreach ($server in $servers){
if (test-connection $server -quiet){
{
Do stuff
}
}

by Zushakon at 2012-09-05 04:19:37

Continue statment works like a charm, thank You for help.

I know i could use the if statment but allready i got so many loops with diffrent if that i wanted to make it a bit easier for the eye.