从用户属性中删除“成员”选项卡中的组

时间:2012-07-06 14:32:20

标签: powershell powershell-v2.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}

但问题是,如果此脚本无法从tab的用户成员中删除任何组,则会在下面引发错误,但它不会显示哪个用户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

2 个答案:

答案 0 :(得分: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
}
}

答案 1 :(得分: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
        }
    }
}}