Powershell--switch

Source: Internet
Author: User

# Learn from function switch


< #示例 1:


1.1 Returns the result of 1 if the value of the variable value matches the number 1 below (the default comparison operator is =)


1.2 Give the variable a value 0,1,2,3 to see the effect, respectively.


#>


$value =1


Switch ($value)



{


1 {"Number 1"}


2 {"Number 2"}


3 {"Number 3"}


}


==============================================================================


< #示例2:


2.1 Custom comparison criteria when multiple conditions are met, switch returns multiple values, see "Mastering Powershell"--217 page



2.2 Try 10, 150,210 this, four values to see what results are returned


#>


$value 01 =210


Switch ($value 01)


{

{$_-GT 100} {"$value is greater than"} #注意: If the condition declaration has an expression that requires the {} brace, the custom condition


{"HI"}

{($_-gt)-and ($_-le 300)} {"$value is greater than than 300"}

}



==============================================================================


< #示例3:


3.1 After running example 2, we know that when switch does not meet any of the conditions will not return the results, in fact, we can define the condition is not met, return to the default results


3.2 In fact, as with if else, the following English explanation

In a similar manner as an if statement, the Switch statement executes code only If at least one of

The specified conditions is met. The keyword, which for the IF statement are called Else, is called

Default for Switch statement. When no other condition matches, the default clause is run.


#>



$value 02 = 15 # Try to assign 5,6,7,15 these values to the variable value02, and the results returned




Switch ($value 02)


{


{$_-le 5} {"$_ is a number from 1 to 5"}


6 {"Number is 6"}


{(($_-gt 6)-and ($_-le 10)} {"$_ is a number from 7 to 10"}



Default {"$_ is a number outside the range of from 1 to 10"}


}



< #示例4:


4.1 With the above example, we already know that the switch function returns multiple results when multiple conditions are met, and if I want to return only one result, we can use the keyword


"Break", as long as a condition satisfies the exit declaration (i.e. no longer performing the following conditions)


4.2 English explanations are as follows

If you ' D-to-receive only one result, while consequently making sure, the first applicable

Condition is performed and then append the break statement to the code.


In fact, now you get only the first applicable result. The keyword break indicates this no more

Processing'll occur and the Switch statement'll exit.


#>


$value 03 = 5 # Try assigning 50,60,5 these values to the variable VALUE03 to see the returned results


Switch ($value 03)


{


{"The number"; break} #当我们输入50的时候, 3 conditions are met and 3 results should be returned, but using break will only return one result


{$_-GT 10} {"Larger than"; break}

{$_-is [int]} {"Integer number"; Break}


}



Powershell--switch

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.