【问题标题】:Use -notlike to filter out multiple strings in PowerShell在 PowerShell 中使用 -notlike 过滤掉多个字符串
【发布时间】:2020-01-07 06:18:41
【问题描述】:

我正在尝试读取事件日志以对除两个用户之外的所有用户进行安全审核,但是否可以使用 -notlike 运算符来执行此操作?

是这样的:

Get-EventLog -LogName Security | where {$_.UserName -notlike @("*user1","*user2")}

我让它为单个用户工作,例如:

Get-EventLog -LogName Security | where {$_.UserName -notlike "*user1"}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    V2 至少包含-username 参数,它接受一个字符串[],并且支持通配符。

    V1 你想像这样扩展你的测试:

    Get-EventLog Security | ?{$_.UserName -notlike "user1" -and $_.UserName -notlike "*user2"}
    

    或者您可以在内联数组上使用“-notcontains”,但这只有在您可以对用户名进行精确匹配时才有效。

    ... | ?{@("user1","user2") -notcontains $_.username}

    【讨论】:

    • 您还可以参考用户名列表以通过变量过滤掉:?{$users -notcontains $_.username}
    【解决方案2】:

    我认为彼得的想法是正确的。我会为此使用正则表达式和 -notmatch 运算符。

    Get-EventLog Security | ?{$_.Username -notmatch '^user1$|^.*user$'}
    

    【讨论】:

      【解决方案3】:

      为了支持“匹配任何...”场景,我创建了一个非常易于阅读的函数。我的版本有更多功能,因为它是一个 PowerShell 2.0 cmdlet,但我在下面粘贴的版本应该可以在 1.0 中使用并且没有多余的装饰。

      你这样称呼它:

      Get-Process | Where-Match Company -Like '*VMWare*','*Microsoft*'
      Get-Process | Where-Match Company -Regex '^Microsoft.*'
      
      filter Where-Match($Selector,[String[]]$Like,[String[]]$Regex) {
      
          if ($Selector -is [String]) { $Value = $_.$Selector }
          elseif ($Selector -is [ScriptBlock]) { $Value = &$Selector }
          else { throw 'Selector must be a ScriptBlock or property name' }
      
          if ($Like.Length) {
              foreach ($Pattern in $Like) {
                  if ($Value -like $Pattern) { return $_ }
              }
          }
      
          if ($Regex.Length) {
              foreach ($Pattern in $Regex) {
                  if ($Value -match $Pattern) { return $_ }
              }
          }
      
      }
      
      filter Where-NotMatch($Selector,[String[]]$Like,[String[]]$Regex) {
      
          if ($Selector -is [String]) { $Value = $_.$Selector }
          elseif ($Selector -is [ScriptBlock]) { $Value = &$Selector }
          else { throw 'Selector must be a ScriptBlock or property name' }
      
          if ($Like.Length) {
              foreach ($Pattern in $Like) {
                  if ($Value -like $Pattern) { return }
              }
          }
      
          if ($Regex.Length) {
              foreach ($Pattern in $Regex) {
                  if ($Value -match $Pattern) { return }
              }
          }
      
          return $_
      
      }
      

      【讨论】:

        【解决方案4】:

        不要使用 -notLike,-notMatch 与正则表达式一起工作:

        Get-MailBoxPermission -id newsletter | ? {$_.User -NotMatch "NT-AUTORIT.*|.*-Admins|.*Administrators|.*Manage.*"}
        

        【讨论】:

        • 我很好奇,这个例子中 '.' 的目的是什么?
        【解决方案5】:

        我为多个搜索找到的最简单方法是将它们全部通过管道传输(可能更重的 CPU 使用)但对于您的示例用户:

        Get-EventLog -LogName Security | where {$_.UserName -notlike "*user1"} |  where {$_.UserName -notlike "*user2"}
        

        【讨论】:

          【解决方案6】:

          场景: 列出所有以 XX1 开头的计算机,但不列出第 4 个字符为 L 或 P 的名称

          Get-ADComputer -Filter {(name -like "XX1*")} | Select Name | Where {($_.name -notlike "XX1L*" -and $_.name -notlike "XX1P*")}
          

          您还可以通过将上述脚本括在括号中并添加一个 .count 方法来计算它们,如下所示:

          (Get-ADComputer -Filter {(name -like "XX1*")} | Select Name | Where {($_.name -notlike "XX1L*" -and $_.name -notlike "XX1P*")}).count
          

          【讨论】:

            【解决方案7】:
            $listOfUsernames = @("user1", "user2", "etc", "and so on")
            Get-EventLog -LogName Security | 
                where { $_.Username -notmatch (
                    '(' + [string]::Join(')|(', $listOfUsernames) + ')') }
            

            我承认这有点疯狂,它无法转义用户名(在不太可能的情况下,用户名使用正则表达式转义字符,如 '\' 或 '(' ),但它可以工作。

            正如上面提到的“slipsec”,尽可能使用-notcontains。

            【讨论】:

            • 谢谢彼得.. 这也是一个很好的选择!
            【解决方案8】:

            使用选择字符串:

            Get-EventLog Security | where {$_.UserName | select-string -notmatch user1,user2}
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-09-15
              • 1970-01-01
              • 2022-09-23
              • 2018-09-28
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多