• PowerShell笔记


    本系列是一个重新学习PowerShell的笔记,内容引用自PowerShell中文博客

    条件操作符

    比较

    Powershell 中的比较运算符
    -eq :等于
    -ne :不等于
    -gt :大于
    -ge :大于等于
    -lt :小于
    -le :小于等于
    -contains :包含
    -notcontains :不包含
    
    PS C:PowerShell> 1 -eq 1                                                                                               True
    PS C:PowerShell> $arr = 1..10                                                                                          PS C:PowerShell> $arr -contains 5                                                                                      True
    PS C:PowerShell> $arr -lt 5                                                                                            1
    2
    3
    4
    

    取反

    求反运算符为-not 但是像高级语言一样”! “ 也支持求反。
    
    PS C:PowerShell> $r = $arr -contains 5                                                                                 PS C:PowerShell> $r                                                                                                    True
    PS C:PowerShell> -not $r                                                                                               False
    PS C:PowerShell> !$r                                                                                                   False
    

    布尔运算

    -and :和
    -or :或
    -xor :异或
    -not :逆
    
    PS C:Powershell> $true -and $true
    True
    PS C:Powershell> $true -and $false
    False
    PS C:Powershell> $true -or $true
    True
    PS C:Powershell> $true -or $false
    True
    PS C:Powershell> $true -xor $false
    True
    PS C:Powershell> $true -xor $true
    False
    PS C:Powershell>  -not  $true
    False
    

    过滤器 Where-Object

    -EQ
    
    -Match 
    
    -CEQ 
    
    -NE 
    
    -CNE 
    
    -GT 
    
    -CGT 
    
    -LT 
    
    -CLT 
    
    -GE 
    
    -CGE 
    
    -LE 
    
    -CLE 
    
    -Like 
    
    -CLike 
    
    -NotLike 
    
    -CNotLike 
    
    -CMatch 
    
    -NotMatch 
    
    -CNotMatch 
    
    -Contains 
    
    -CContains 
    
    -NotContains 
    
    -CNotContains 
    
    -In 
    
    -CIn 
    
    -NotIn 
    
    -CNotIn 
    
    -Is 
    
    -IsNot 
    
    PS C:PowerShell> Get-Process | Where-Object -FilterScript {$_.Name -like "chrome"}                                                                                                          
    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    -------  ------    -----      -----     ------     --  -- -----------
        321      17    25288      44540       2.27   2520   1 chrome
        329      19    31924      78012       4.41   2692   1 chrome
    

    条件表达式

    If

    PS C:PowerShell> If($arr -notcontains 11 ){"都不包含11"}ElseIf($arr -gt 10){"大于10"}Else{"都不满足"}                                                                                       
    都不包含11
    

    Switch

    $TValue = 100 
    $domain="www.mossfly.com"
    #多值匹配
    switch ($TValue)
    {
        {$_ -lt 200 }   { "小于5"}
        {$_ -gt 10 }   { "大于10"}
        {$_ -in 100,200,300} {"存在于100,200,300"}   
        Default {"匹配失败"}
    }
    
    "第二次匹配"
    #匹配后跳出
    switch ($TValue)
    {
        {$_ -lt 200 }   { "小于5跳出";break}   
        {$_ -in 100,200,300} {"存在于100,200,300"}   
        Default {"匹配失败"}
    }
    
    "大小写敏感"
    
    #大小写敏感
    switch -case ($domain)
    {
        "Www.moSSfly.com" {"Ok 1"}
        "www.MOSSFLY.com" {"Ok 2" }
        "www.mossfly.com" {"Ok 3"}
    }
    
    "使用通配符"
    
    #使用通配符
    switch -wildcard($domain)
    {
        "*"     {"匹配'*'"}
        "*.com" {"匹配*.com" }
        "*.*.*" {"匹配*.*.*"}
    }
    
    "正则匹配"
    #使用正则
    switch -regex ($mail)
    {
        "^www"     {"www打头"}
        "com$"     {"com结尾" }
        "d{1,3}.d{1,3}.d{1,3}.d{1,3}" {"IP地址"}
    }
     
    PS C:PowerShell> .test.ps1                                                                                                                                                 小于5
    大于10
    存在于100,200,300
    第二次匹配
    小于5跳出
    大小写敏感
    Ok 3
    使用通配符
    匹配'*'
    匹配*.com
    匹配*.*.*
    正则匹配
    
  • 相关阅读:
    人工神经网络(Artificial Neural Networks)
    潜语义分析(Latent Semantic Analysis)
    IOS Dictionary和Model相互转换
    jquery ajax跨域请求webservice
    日期格式转换
    1
    iptables详解
    yum报错-Network is unreachable"Error:
    41个Web开发者JavaScript实用小技巧
    比较常用的几个maven第三方镜像
  • 原文地址:https://www.cnblogs.com/MerLin-LiuNian/p/15263403.html
Copyright © 2020-2023  润新知