Microsoft Exchange 2010 PowerShell Cookbook
上QQ阅读APP看书,第一时间看更新

Using flow control statements

Flow control statements are used in the shell to run one or more commands based on the result of a conditional test. You can use the If statement to test one or more conditional statements, and you can also use switch statements when multiple If statements would otherwise be required. This recipe will show you how to control the flow of execution that your scripts will use in the shell.

How to do it...

Let's store the status of a database called DB1 in a variable that can be used to perform some conditional checks:

$DB1 = Get-MailboxDatabase DB1 -Status

When using an If statement, you use the If keyword followed by an expression enclosed in parenthesis that performs a conditional check. If the expression is evaluated as true, any commands in the proceeding script block will be executed:

if($DB1.DatabaseSize -gt 5gb) {
  "The Database is larger than 5gb"
}

You can use the ElseIf keyword to add another conditional check:

if($DB1.DatabaseSize -gt 5gb) {
  "The Database is larger than 5gb"
}
elseif($DB1.DatabaseSize -gt 10gb) {
  "The Database is larger than 10gb"
}

You can also add the Else statement to run commands if none of the conditions evaluate as true:

if($DB1.DatabaseSize -gt 5gb) {
  "The Database is larger than 5gb"
}
elseif($DB1.DatabaseSize -gt 10gb) {
  "The Database is larger than 10gb"
}
else {
  "The Database is not larger than 5gb or 10gb"
}

If you need to check more than a few conditions, you may want to consider using a switch statement instead of series of If and ElseIf statements:

switch($DB1.DatabaseSize) {
  {$_ -gt 5gb}  {"Larger than 5gb"; break}
  {$_ -gt 10gb} {"Larger than 10gb"; break}
  {$_ -gt 15gb} {"Larger than 15gb"; break}
  {$_ -gt 20gb} {"Larger than 20gb"; break}
  Default       {"Smaller than 5gb"}
}

How it works...

To control the flow and execution of commands in your scripts, you can use the If, Elseif, and Else conditional statements. The syntax of an If statement is pretty straightforward. Let's break it down in simple terms. In the first example, we're simply asking PowerShell if the database size of DB1 is greater than five gigabytes, and, if it is, to output a string with the message "The database is larger than 5gb".

In the second example, we extend this logic by simply asking another question: if the database size of DB1 is greater than 10 Gigabytes, output a string with the message "The database is larger than 10gb".

Next, we use an Else statement that will only run commands if either the If or ElseIf statements do not evaluate to true. If that's the case we simply output a string with the message "The database is not larger than 5gb or 10gb".

One interesting thing to point out here is that the code within parenthesis is like any other expression we might type into the shell. There's no requirement to first create a variable, as shown previously. We could just do something like this:

if((Get-MailboxDatabase DB1 -Status).DatabaseSize -gt 5gb) {
  "The database is larger than 5gb"
}

Since we know that the Get-MailboxDatabase cmdlet can return an object with a DatabaseSize property, we can simply wrap the command in parenthesis and access the property directly using dot notation. This is a technique that can cut down on the amount of code you write and greatly speed up your work when you are typing commands interactively into the shell.

It's possible to use multiple ElseIf statements to run a series of multiple conditional checks, but the switch statement is much better suited for this task. The switch statement syntax may be a little harder to understand. After using the switch keyword, you specify the object that you want to perform multiple conditional checks against. Each line within the body of the switch can evaluate an expression or check for a precise value. If an expression evaluates to true or a match is found, any commands in the associated script block will run.

In our previous example, we evaluated a number of expressions to determine if the size of the database was greater than a specific value. Notice that in each script block we used the break keyword. This means that we exit the switch statement immediately after an expression has been evaluated as true and any following checks will be skipped. Finally, the last item in the switch uses the Default keyword which will only run if the previous expressions are false.

You can also use a switch statement that will run commands when matching a specific value. Take a look at the following code:

$number = 3

switch ($number) {
  1 {"One" ; break}
  2 {"Two" ; break}
  3 {"Three" ; break}
  4 {"Four" ; break}
  5 {"Five" ; break}
  Default {"No matches found"}
}

In this example, the $number variable is set to 3. When the switch statement runs, the word Three will be returned. If $number had been set to a value that was not defined, such as 42, the Default script block would run and output the string "No Matches Found".

Switch statements can also be used to perform complex matches with regular expressions, wildcards, exact matches, case sensitive values, and data read in from external files. For more details, run Get-Help About_Switch.

There's more...

Let's take a look at a more practical example of how you might use flow control statements in a real script. Here we'll loop through each mailbox in the organization to configure some of the mailbox quota settings:

foreach ($mailbox in Get-Mailbox) {
  if($mailbox.office -eq "Sales") {
   Set-Mailbox $mailbox -ProhibitSendReceiveQuota 5gb `
   -UseDatabaseQuotaDefaults $false
  }
  elseif($mailbox.office -eq "Accounting") {
   Set-Mailbox $mailbox -ProhibitSendReceiveQuota 2gb `
   -UseDatabaseQuotaDefaults $false
  }
  else {
   Set-Mailbox $mailbox -UseDatabaseQuotaDefaults $true
  }
}

In this example we are checking to see if the Office setting for each mailbox is set to "Sales" using the If statement. If so, the ProhibitSendReceiveQuota is set to five gigabytes. If not, the ElseIf statement will check that the Office setting is set to "Accounting", and, if it is, the ProhibitSendReceiveQuota is set to two gigabytes. If the Office setting is not set to either of these values, we can configure the mailbox to use database quota defaults.

Note

Notice the use of the back tick (`) character used in the previous example with the Set-Mailbox cmdlet. This can be used as a line continuation character to break up long commands into multiple lines.

See also

  • Looping through items