Executive Summary:
Microsoft Exchange Server 2007's Exchange Management Shell lets you construct Windows PowerShell scripts for common Exchange Server tasks that you'll need to run again and again. For evaluating values or conditions in scripts, you use relational operators and logical operators along with code such as if statements or switch statements. To loop through a block of code in your scripts, you use code such as the for statement, foreach statement, or while statement.
|
Microsoft Exchange Server 2007's Exchange Management Shell (EMS) is a good interactive environment: You enter commands, the shell checks them for syntax and executes them if possible, and you see the output immediately. That's a fine model when you need to make a series of changes once or if you just need to bang out a couple of commands quickly to make something happen. However, there are many circumstances where you really want to take a group of commands, glue them together with some logic that makes decisions or evaluates conditions, and save the resulting script so you can run it at any time.
In previous articles in this series, I've shown the basics of how to use Windows PowerShell commands through EMS to retrieve Exchange Server objects, inspect their properties, and make changes to one or more objects. (See the Learning Path on the right side of the page for the other articles in this series as well as additional articles on PowerShell and Exchange management with PowerShell.) Everything we've done so far has focused on entering commands in EMS and having them immediately executed. In this article, I'll show you how to construct scripts, which you can run over and over again to automate common tasks such as getting mailbox information, creating users, and updating configuration settings across multiple servers.
Bear in mind that we're still talking about Exchange Server 2007, which uses PowerShell 1.0. PowerShell 2.0, now available as a Community Technology Preview (CTP) from Microsoft's website, includes a GUI editor that simplifies some aspects of scripting, but you can't use it just yet with Exchange.
Evaluating Conditions and Making Decisions
The interactive EMS examples I've presented in previous articles rely on the basic 3-step pattern of getting objects, filtering for the ones that should be acted upon, then taking some kind of action. One of the key differences between interactive commands and scripts is that scripts usually need a way to evaluate conditions and take actions based on the results of those evaluations.
Scripting languages typically provide relational operators that let you compare two values to see whether one is greater than, less than, or equal to the other. EMS has a range of operators, as Table 1 shows, that you can use for comparisons. These operators work on most built-in data types, including strings. However, string comparisons in EMS aren't case-insensitive by default. If you need case-sensitive comparisons, you can get them by prefixing the operator you want with the letter c. For example,
"Paul" -ceq "paul"
returns false because the two strings aren't exactly equal.

You can also use logical operators for comparisons or to change the meaning of other types of operations. These operators, shown in Table 2, are commonly used in conditional statements where a different action is taken depending on the result. There are other, more advanced logical operators as well, but the ones here should cover most needs.

You have several ways to use conditional operators in EMS to enable your scripts to evaluate conditions and take actions on the results. The first, and simplest, method is the if statement. This procedure will be immediately familiar to anyone who's written any kind of script before: You specify a condition to test and an action to take. You can also include an else clause, which will be evaluated only if the initial condition is false. Here's an example:
If (True)
Write-Host "PowerShell is AWESOME!"
Else
Write-Host "You'll never see this message."
You can also make use of the elseif statement to chain together multiple tests:
$detroitLions = FALSE;
$newOrleansSaints = TRUE;
If ($detroitLions)
Write-Host "Well, there's always next year"
ElseIf ($newOrleansSaints)
Write-Host "Lifelong Saints fans never give up"
Else
WriteHost "Not a football fan, I guess!"
The if statement is useful in many circumstances, but it's not the best way to perform tests where you want to evaluate one value against several possible choices. In that case, you use the switch statement, which lets you specify a set of possible values and a block of code to be executed for each. As an example, you could construct a switch statement to evaluate the odds of a particular NFL team going to the Super Bowl:
Switch ($teamName)
{
"Saints" {$odds = 0.5; WriteHost "Maybe...";}
"Lions" { $odds = 0.0; WriteHost "No way!";}
"Giants" {$odds = 0.9; WriteHost "Looks likely at this point.";}
}
Note that in this example, the code executed for each case of the switch statement contains more than one statement, separated by semicolons and enclosed by curly braces. Braces are an important piece of EMS syntax. You can enclose multiple statements in braces to have them treated as a group in loops and conditional statements.
You might also have noticed that the switch statement above uses string comparisons. EMS also lets you use range comparisons, regular expressions, and wildcards as part of the individual switch statements. For example, you can write the following switch statement to convert from a numerical grade to a letter grade:
Switch ($testScore)
{
{ $_ -lt 60} {$letterGrade = "F"; break;}
{$_-lt 70} {$letterGrade = "D"; break; }
{$_-lt 80} {$letterGrade = "C"; break; }
{$_-lt 90} {$letterGrade = "B"; break; }
{$_-lt 100} {$letterGrade = "A"; break; }
}
Note that each comparison in the switch statement uses the $_ token to represent the current object, along with a relational operator.