0

我是powershell的新手 下面的脚本我从选项卡的用户成员中删除了所有组,除了“域用户”这很多次都可以正常工作,没有任何问题

从用户中删除组

$list = Import-Csv c:\user\DN.csv
foreach ($entry in $list)
$UserDN = $entry.DistinguishedName
Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {
if ($_.name -ne "Domain Users") {remove-adgroupmember -identity $_.name -member   $UserDN -Confirm:$False}

但问题是,如果此脚本无法从选项卡的用户成员中删除任何组,它会在下面引发错误,但它不会显示从哪个用户 ID 无法删除成员资格,因为用户专有名称是从中导入的CSV 文件。

Remove-ADGroupMember : The specified account name is not a member of the group
At C:\User\removegroups.ps1:35 char:115
+ Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {if ($_.name -ne "Domain    Users") {remove-adgroupmember <<
<<  -identity $_.name -member $UserDN -Confirm:$False}
    + CategoryInfo          : NotSpecified: (xyz:ADGroup) [Remove-ADGroupMember],     ADException
    + FullyQualifiedErrorId : The specified account name is not a member of the     group,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
4

2 回答 2

0
$list = Import-Csv c:\user\DN.csv
foreach ($entry in $list)
$UserDN = $entry.DistinguishedName
Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {
if ($_.name -ne "Domain Users") {
try {
remove-adgroupmember -identity $_.name -member $UserDN -Confirm:$False} }
catch [ADexcption] {
write-output "Error Deleting User:" $_.name
}
}
于 2012-07-06T18:30:16.567 回答
0
Import-Csv DN.csv | foreach {
$user = Get-ADUser $_.username
$UserDN = $user.DistinguishedName
Get-ADGroup -LDAPFilter "(member=$UserDN)" | foreach-object {
    if ($_.name -ne "Domain Users") {
        try {
            remove-adgroupmember -identity $_.name -member $UserDN -Confirm:$False
        }
        catch [ADexcption] {
            write-output "Error Deleting User:" $_.name
        }
    }
}}
于 2015-11-10T13:29:11.840 回答