【问题标题】:Get AD-Computer not like distinguished name获取 AD-Computer 不像专有名称
【发布时间】:2020-08-04 16:11:06
【问题描述】:

我正在尝试从 AD 获取计算机列表,不包括一些不再使用的计算机。 这是我的代码:

$ServerList = Get-ADComputer -Filter * | Where { 
    $_.DistinguishedName -like "*Computers*" -and $_.DistinguishedName -notlike @("*server1*","*Server2*")
} | Select-Object Name 

我正在尝试将要排除的计算机放入数组中,而不是使用

-and $_.DistinguishedName -notlike "*serverIwantToExclude*"

你们能告诉我如何修改它吗?

【问题讨论】:

    标签: windows powershell active-directory


    【解决方案1】:

    -notlike 不支持右侧的集合 (RHS)。类似的方法是使用-notmatch,它是一个正则表达式字符串:

    $ServerList = Get-ADComputer -Filter * |
        Where { $_.DistinguishedName -like "*Computers*" -and $_.DistinguishedName -notmatch 'server1|Server2'} |
            Select-Object Name
    

    如果您希望您的服务器名称首先出现在列表中,您可以从中创建一个正则表达式字符串。

    $serverdown = 'server1','server2'
    $regex = $serverdown -join '|'
    $ServerList = Get-ADComputer -Filter * |
        Where { $_.DistinguishedName -like "*Computers*" -and $_.DistinguishedName -notmatch $regex} |
            Select-Object Name
    

    如果您不锚定您的正则表达式字符串,它会在目标字符串中的任何位置查找正则表达式匹配项(有效地包含周围的通配符)。 | 是一个替代(有效的OR)。

    还有其他支持集合的运算符,例如 -contains-in-notin-notcontains。但是,它们必须完全匹配,并且不能使用通配符。

    【讨论】:

    • 嗨,谢谢它有效.. $serverdown = @('server1','server2') 有没有办法像下面这样放入数组中? $ServerList = 获取 ADComputer -Filter * |其中 { $_.DistinguishedName -like "Computers" -and $_.DistinguishedName -notmatch $serverdown} |选择-对象名称
    • 是的。我添加了如何使其适应使用数组。
    【解决方案2】:

    如果您知道要排除的服务器的全名,则可以使用-inotin 运算符,这意味着您不能将通配符或正则表达式与-inotin 运算符一起使用。如果是这种情况,并且您知道我建议使用 ADComputer 对象的 Name 属性的名称。

    [string[]] $excludedServers = 'server1','server2'
    
    $ServerList = Get-ADComputer -Filter * | Where { 
        $_.DistinguishedName -like "*Computers*" -and $_.Name -inotin $excludedServers 
    } | Select-Object Name 
    

    【讨论】:

    • 谢谢,现在更有意义了 :)
    【解决方案3】:

    排除它们的另一种方法是将所有这些计算机添加到一个组中并排除它

    #提供组的DN

    $groupname = "CN=groupname,OU=Groups,DC=Domain,DC=COM"

    get-adcomputer -filter {(OperatingSystem -like "Windows")} | -属性 * | ?{([string]$_.memberof -notmatch $groupname)} |选择姓名,上次登录日期

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多