这个需求可以通过两个方向来实现
1、找到禁用的账号,删除除domain users外的所有组,脚本内容如下
#导入AD模块
import-module ActiveDirectory
#被禁用户
$users = get-aduser -Filter * -SearchBase "OU=xxx,DC=xxx,DC=com" | foreach {if ($.enabled -eq $false){echo $.Name} }
#删除用户所有组
foreach($user in $users){
$Membership = Get-ADPrincipalGroupMembership $User
$group = $Membership.distinguishedName -ne "CN=Domain Users,CN=Users,DC=xxx,DC=com"
Remove-ADPrincipalGroupMembership -identity $User -MemberOf $group -confirm:$False
}
2、找到禁用账号的samaccountname跟组,将domain users外的组通过samaccountname删除成员,内容如下
import-module ActiveDirectory
$users = get-aduser -filter 'enabled -eq $false' -Properties samaccountname, memberof -SearchBase "OU=xxx,DC=xxx,dc=com" | select samaccountname,
@{ n = 'MemberOf'; e = { ($.memberof | % { (Get-ADObject $).Name }) -join "," } }
Foreach ($user in $users)
{
Get-ADGroup -Filter { name -notlike "domain users" } | Remove-ADGroupMember -Members $user.samaccountname -Confirm:$False
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。