Exchange the PowerShell control statement

Source: Internet
Author: User

in the flow control statement, we use the Shell to run one or more commands based on the results of the condition test. You can use the if statement to test one or more conditional statements , or you can use the switch statement, a section that discusses how to use a flow-control statement

Let's take a look at the example

First I create a variable that can be used for condition checking:

when using a if statement, if you are using a keyword that performs a condition check, the script block is executed if the value of the expression is true $DB 1 = get-mailboxdatabase "Mailbox Database 1014271532" –status

if ($DB 1. Databasesize-gt 100mb) {

"The Database is larger than 100MB"

}

650) this.width=650; "title=" 01.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/28/ Wkiol1tjao-c3dejaaczags6pky438.jpg "alt=" Wkiol1tjao-c3dejaaczags6pky438.jpg "/>

You can also use ElseIf keyword Add other condition check

if ($DB 1. Databasesize-gt 50mb) {

"The Database is larger than 5GB"

}

ElseIf ($DB 1. Databasesize-gt 100mb) {

"The Database is larger than 10GB"

}

If no condition evaluation is correct, you can also add Else Statement

if ($DB 1. Databasesize-gt 50mb) {

"The Database is larger than 50MB"

}

ElseIf ($DB 1. Databasesize-gt 100mb) {

"The Database is larger than 100MB"

}

else {

"The Database is not larger than 50MB or 100mb"

}

If the check has multiple conditions, we can consider using the Switch statement, rather than a series of if and the Elseif Statement

Switch ($DB 1. Databasesize) {

{$_-GT 50MB} {"Larger than 50MB"; break}

{$_-GT 100MB} {"Larger than 100MB"; break}

{$_-GT 300MB} {"Larger than 300MB"; break}

{$_-GT 500MB} {"Larger than 500MB"; break}

Default {"Smaller than 50MB"}

}

when executing a script, use the control flow statement, which can be if , Elseif and the Else The conditional statement.

keep looking at the following example, you can use a Switch statement, the ability to command when matching a specific value, see the following code:

$number = 3

Switch ($number) {

1 {"One"; break}

2 {"n"; break}

3 {"Three"; break}

4 {"Four";

5 {"Five"; break}

Default {"No matches found"}

}

Look at the results of the operation:

650) this.width=650; "title=" 02.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/2A/wKiom1TJAA_ J7crkaaczvet9zza966.jpg "alt=" Wkiom1tjaa_j7crkaaczvet9zza966.jpg "/>

in this example, the variable $number is set to the value of 3 , when Switch when the statement runs, the word three will be returned. However, if the value of the variable is not defined, for example,therun result of this script will show no Matches found

Switch statements can be used to perform complex regular expression matches, wildcard characters, exact matches, and case sensitive values, and more about Switch statement can use the Help system get-help About_switch

Let's look at a more practical example of how to use a flow control statement to traverse the quota settings for each mailbox in your organization's configuration in a single script

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

}

}

650) this.width=650; "title=" 03.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/28/ Wkiol1tjapdhlinraao7eopu34g849.jpg "alt=" Wkiol1tjapdhlinraao7eopu34g849.jpg "/>

if statement to check the office of each mailbox (Office) is sales the mailbox, if yes, then the prohibitsendreseivequota configured to 5GB, elseif statement checks whether the office is accounting , if it is, put prohibitsendreceivequota configured to 2GB

This article from "Robin's Home" blog, declined reprint!

Exchange the PowerShell control statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.