2012-07-06 93 views
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 

回答

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

我已經嘗試了很多,但這個工作不低於錯誤顯示嘗試語句丟失其catch或finally塊。 你能幫忙解決這個問題嗎? – user1506975 2012-07-09 12:03:38

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