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

ddrv8njm  于 2023-01-17  发布在  Shell
关注(0)|答案(2)|浏览(153)

我是新的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
zy1mlcev

zy1mlcev1#

$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
}
}
pu3pd22g

pu3pd22g2#

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
        }
    }
}}

相关问题