Exchange PowerShell Quickstart – Part 4

Over the past three parts we have covered connecting to Exchange and discovering cmdlets in part 1. We explored the pipeline in part 2 followed by hash tables, arrays and variables in part 3. This week we’re covering flow control in terms of conditional and iterative processing.

We covered where (?) and foreach (%) as part of the pipeline discussion part 2, but we can write small scripts that can be a bit easier to follow than a single long pipeline command. Personally I love the pipeline for working directly within PowerShell, but for anything that needs to be run many times over, or handed over for others to use or support, then I write small scripts instead. I still use the pipeline (|) a little bit in scripts but only to optimize things like mermory usage, which is a more advanced area of discussion.

Conditional Processing

There are two constructs for conditional processing. if/else and switch, both perform similar tasks of only executing a piece of script if some set of conditions is met

if/else

$aVariable = 'droid'
if( $aVariable -eq 'droid' ) {
write-host 'We have a droid'
} else {
write-host 'there are no droids here'
}

You can get more complicated with adding multiple logical expressions, such as -or, -and, -not, -lt, etc, as we looked at in part 2. You can also have more than just if and else

$aVariable = 'droid'
if( $aVariable -eq 'droid' ) {
   write-host 'We have a droid'
} elseif( $aVariable -eq 'humanoid' ) {
   write-host 'We have a humanoid'
} else {
   write-host 'there are no droids here'
}

Switch

Of course as we want to add more and more cases then that starts to look a bit more cumbersome, so then we have the swtich statement. If we take the above example and write it as a switch we get

$aVariable = 'droid'
switch( $aVariable ) {
   'droid' {
      write-host 'We have a droid'
      break
   }
   'humanoid' {
      write-host 'We have a humanoid'
      break
   }
   default {
      write-host 'there are no droids here'
   }
}

The observant amongst you will have noticed a special keyword ‘break’ This ensures that Powershell doesn’t just fall down into the next switch condition and breaks out of the switch. For direct equality conditions that won’t be an issue but as you start to write switch statements that use wildcards or regular expressions it is safest to always put in an explicit break statement

Iterative Processing

There are four iterative processing constructs in PowerShell. These are foreach, for, while and do. They all iterate through a set of data and process each one, but they do slightly differ.

Foreach

Foreach is similar to the ForEach-Object (or %) used on the command line, but it allows us to more narratively access each occurrence.

$Mailboxes = Get-Mailbox
Foreach( $Mailbox in $Mailboxes ) {
   Write-Host $Mailbox.DistinguishedName
}

In the above trivial example we just iterate through the mailboxes and print out the distinguished name, and it is a very readable format. Even if you didn’t know PowerShell you could guess what it would do

For

The For statement is generally more dedicated to counter loops, where you want something to happen say 10 times

for( $i=0; $i -lt 10; $i++ ) {
   Write-Host $i
}

While

The while statement runs the commands within the while statement as long as the condition is met

$i = 0
while( $i -lt 10 ) {
   Write-Host $i
   $i++
}

Do

The Do statement is similar to the while excpet it always runs at least one time, irrespective of whether the condition is met or not. So in the example below we’d still write out the value 11

$i = 11
Do{
   Write-Host $i
   $i++
} while ( $i -lt 10 )

In Summary

Once you start writing scripts for others to use then you will want to become familiar with conditional and iterative processing, and continue to use narative names for your variables so that the resulting script is largely self documenting. Next time we will dive into Error Handling with PowerShell

See you all next time!

Twan van Beers

Twan is a senior consultant with over 20 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.

Leave a Reply

Your email address will not be published. Required fields are marked *

Search