9. PowerShell-Conditional control and logical judgment

Source: Internet
Author: User

    • PowerShell Condition Control

  1. If, Else, ElseIf statement

    Basic syntax:

    if ( condition )

    { Code }

    ElseIf ( condition )

    { Code }

    Else ( conditional )

    { Code }

    Else

    { Code }

    For example 1:

    $n =10

    if ($n-eq 1)

    {"N=1"}

    ElseIf ($n-ne 1)

    {"N!=1,and n= $n"}

    Results: {"N!=1,and n= $n"}

    Example 2: View the operating system of this computer

    $ComputerName = (Get-wmiobject-class win32_computersystem). Name

    $OS _version = (Get-wmiobject-class win32_operatingsystem-computer$computername). Version

    if (($OS _version-eq "5.1.2600")-or ($OS _version-eq "5.2.3790"))

    {

    Write-host "ComputerName:" $ComputerName

    if ($OS _version-eq "5.1.2600")

    {

    Write-host "Os:windowsxp"

    }

    ElseIf ($OS _version-eq "5.2.3790")

    {

    Write-host "os:windows2003"

    }

    }

    ElseIf (($OS _version-eq "5.0.2195")-or ($OS _version-eq "6.1.7600"))

    {

    Write-host "ComputerName:" $ComputerName

    if ($OS _version-eq "5.0.2195")

    {

    Write-host "os:windows2000 Server"

    }

    ElseIf ($OS _version-eq "6.1.7600")

    {

    Write-host "Os:windows7"

    }

    }

    Else

    {

    Write-host "$ComputerNameis not supported."

    }

    "–end of report–"

    Results:

    Computer1 is not supported.

    -end of report-

    PS c:\>

    PS c:\> $OS _version

    6.3.9600

  2. Switch Statement

    Basic syntax:

    Switch ( expression )

    {

    ( expression ) { Code }

    value { code }

    Default { execution code }

    }

    For example , we can query the value of "DomainRole" to determine the role that the computer holds in the current activedirectory domain, try the following code:

    Switch ((Get-wmiobject-class win32_computersystem). DomainRole)

    {

    0 {write-host "standaloneworkstation"}

    1 {write-host "Memberworkstation"}

    2 {write-host "Standaloneserver"}

    3 {write-host "Memberserver"}

    4 {write-host "Backup DomainController"}

    5 {write-host "Primarydomain Controller"}

    Default {write-host "Cannotdetermine domain Role"}

    }

Run the result on the member computer:"Member workstation"

    • PowerShell Logical Judgment

  1. introduction

    - EQ determine if the equals (equal)

    -lt than

    -gt determine if greater than ( Greater than)

    -ge Determine if it is greater than or equal to (greater of Equal)

    -le Determine if it is less than or equal to (lesser or equal)

    P style= "Color:rgb (0,0,0);" >-ne (no equal)

    -ieq ( case-insensitive)

    -ceq ( case-sensitive)

     

  2. instance

    " A "–eq" a " result : true

    : true

    result : False

  3. Logical operations

    -and and

    -or or

    -not Non-

    ! Non-

Reference: http://marui.blog.51cto.com/1034148/293907



This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1639194

9. PowerShell-Conditional control and logical judgment

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.