【问题标题】:Searching for AD Group Membership搜索 AD 组成员
【发布时间】:2020-02-10 19:41:09
【问题描述】:

我正在构建一个脚本来查找未登录 X 天且不属于特定安全组的 AD 用户。我在 OU 中有 2 个用户,其中一个在 DoNotDisable 安全组 (pileum) 中,一个不在 (bubba) 中。

$DC = Get-ADDomainController
$OUs = Get-ADOrganizationalUnit -Filter 'Name -eq "test"'
$accounts = $null
$canNotDisable = Get-ADGroupMember -Identity DoNotDisable -Recursive | Select -ExpandProperty Name
$TimeStamp = get-date -format D
$description = "Disabled on " + $TimeStamp

foreach ($OU in $OUs) {
    # Search for User Accounts inactive for XX Days and Disable if not in DoNotDisable Security Group
    $accounts = Search-ADAccount -SearchBase $OU -AccountInactive -TimeSpan ([timespan]90d) -UsersOnly
        foreach($account in $accounts){
        If ($canNotDisable -contains $account){
        Write-Host "$account can not be disabled"
        } Else {
        Write-Host "$account can be disabled"
        }
    }
}

如果我查看 $canNotDisable 变量,它会在 DoNotDisable 组中拉取正确的用户。

但是,当我运行完整的脚本时,它会返回组中的用户和不在组中的用户。

如果有人能帮我弄清楚我缺少什么,我将不胜感激。 TIA。

【问题讨论】:

    标签: powershell powershell-5.0 active-directory-group


    【解决方案1】:

    更改内部 foreach 循环中的 if 语句以匹配 ID,而不是整个对象。

    由于$canNotDisable 是一个字符串列表,您需要从$Account 变量中获取名称以查看它是否存在(而不是生成的对象)。

    foreach ($OU in $OUs) {
        # Search for User Accounts inactive for XX Days and Disable if not in DoNotDisable Security Group
        $accounts = Search-ADAccount -SearchBase $OU -AccountInactive -TimeSpan ([timespan]90d) -UsersOnly
            foreach($account in $accounts){
            If ($canNotDisable -contains $account.Name){
            Write-Host "$account can not be disabled"
            } Else {
            Write-Host "$account can be disabled"
            }
        }
    }
    

    【讨论】:

    • 非常感谢。我觉得我 99% 都在那里,非常感谢你让我到达终点线。
    【解决方案2】:

    我认为这里的问题是 Get-ADGroupMember 返回的对象与 Get-ADUser 返回的对象类型不同。它们都代表 AD 用户,但对象并不等价,令人惊讶的是,这些类型的对象并没有进行比较以正确工作。

    如果这是问题所在,我希望首先从 $canNotDisable 列表中获取 ObjectGUID 列表(例如,$canNotDisableGuids = $canNotDisable | %{$_.ObjectGuid}),然后检查每个用户的 ObjectGUID在列表中,可能确实有效。两个对象中的 ObjectGUID 似乎相同。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多