【问题标题】:How can I exclude particular names from Get-ADComputer results?如何从 Get-ADComputer 结果中排除特定名称?
【发布时间】:2014-12-15 19:50:13
【问题描述】:

我想获取我的域中所有已启用的计算机,并且具有 2003 操作系统,并且计算机的名称不包含 ' ping 、 pict 、 pire ' 这是我所拥有的,但完全失败了:

Get-ADComputer -filter {(Enabled -eq $True) -and (OperatingSystem -like "*2003*")} -properties OperatingSystem | where {($_.Name -notlike 'PING*') -or ($_.Name -notlike 'PICT*') -or ($_.Name -notlike 'PIRE*')} | Select Name

【问题讨论】:

  • -or 更改为-and(两者)会得到什么?
  • 定义“失败”。您会得到什么结果,这与您预期的结果有何不同?
  • 您的条件应该是 -and,因为 PICT001 将匹配条件 ($_.Name -notlike 'PING*'),从而使条件的这一部分满足 -or。你也可以用一点正则表达式来简化它:Where-Object { $_.Name -notmatch "^(PING|PICT|PIRE)"}-notmatch "^PI(NG|CT|RE)"}

标签: powershell active-directory


【解决方案1】:

您可以在过滤器中使用-notlike 运算符,因此不需要where 语句。请参阅Get-ADComputer reference on technet

正如我提到的,除了将您的 -or 运算符更改为 -and 之外,我还将所有条件放入过滤器中,结果如下:

Get-ADComputer -filter {
   Enabled -eq $True -and
   OperatingSystem -like '*2003*' -and
   Name -notlike 'PING*' -and
   Name -notlike 'PICT*' -and
   Name -notlike 'PIRE*'
} | Select Name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多