【发布时间】: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