While building out a script there is going to be times when you need to either escape the loop and continue or escape and stop.

Let’s do this by example as its hopefully easier to understand and follow what happens. We need loop over 3 servers and run a command (the command can be anything it doesn’t matter) we decide that we will run a ping to see the server is up before running the command, this way we can say Yes the server is responding, we can run our command. Or Nope its not there lets do X. This is where we have some choices. using with BREAK or CONTINUE you can choose how powershell deals with loops.

There is no Escape

Let’s start with no escapes. Here we just loop over each server, run a try catch to see if we can connect then run our command. As you can see we failed to connect to the server “ThisServerisntreal”

## There is No Escape
$servers = "Server01", "ThisServerIsntReal", "Server02"

Foreach ($server in $servers)
    {
        Write-Host "Connecting to $server"

        TRY
            {
                $conn = Test-Connection $server -Count 1 -ErrorAction Stop
            }
        CATCH
            {
                Write-Host "Failed to connect to: $server" -BackgroundColor Red -ForegroundColor White
            }

        Write-Host "MAKE CHANGE TO SERVER: $server"
        ## Invoke-command DO SOMETHING TO THE SERVER
    }

Output:

Connecting to Server01
MAKE CHANGE TO SERVER : Server01
Connecting to ThisServerIsntReal
Failed to connect to :ThisServerIsntReal
MAKE CHANGE TO SERVER : ThisServerIsntReal
Connecting to Server02
MAKE CHANGE TO SERVER : Server02

As you can, we failed to connect to the server “ThisServerIsntReal” but we still tried to make our change and then carried on to the next server.

Stop Everything! (BREAK)

Let’s say if we can’t connect to one of the servers we want to stop the loop carrying on. In this case we would add some logic into our CATCH statement to say BREAK

# Stop Everything! (BREAK)
$servers = "Server01", "ThisServerIsntReal", "Server02"

Foreach ($server in $servers)
    {
        Write-Host "Connecting to $server"

        TRY
            {
                $conn = Test-Connection $server -Count 1 -ErrorAction Stop
            }
        CATCH
            {
                Write-Host "Failed to connect to: $server" -BackgroundColor Red -ForegroundColor White
                BREAK
            }

        Write-Host "MAKE CHANGE TO SERVER: $server"
        ## Invoke-command DO SOMETHING TO THE SERVER
    }

Output:

Connecting to Server01
MAKE CHANGE TO SERVER : Server01
Connecting to ThisServerIsntReal
Failed to connect to :ThisServerIsntReal
This time, we tried to connect to “ThisServerIsntReal” if failed, so we stopped the loop!

Carry on the loop and escape this iteration (CONTINUE)

If we want to stop the current iteration but carry on the rest of the loop, we would use CONTINUE

# Carry on the loop and escape this iteration (CONTINUE)
$servers = "Server01", "ThisServerIsntReal", "Server02"

Foreach ($server in $servers)
    {
        Write-Host "Connecting to $server"

        TRY
            {
                $conn = Test-Connection $server -Count 1 -ErrorAction Stop
            }
        CATCH
            {
                Write-Host "Failed to connect to: $server" -BackgroundColor Red -ForegroundColor White
                CONTINUE
            }

        Write-Host "MAKE CHANGE TO SERVER: $server"
        ## Invoke-command DO SOMETHING TO THE SERVER
    }

Output:

Connecting to Server01
MAKE CHANGE TO SERVER : Server01
Connecting to ThisServerIsntReal
Failed to connect to :ThisServerIsntReal
Connecting to Server02
MAKE CHANGE TO SERVER : Server02

Our final output see’s that “ThisServerIsntReal” isn’t there and stops the current iteration and moves on to the next server.

Advertisements