我有一個腳本在AD禁用用戶提供了以下步驟:PowerShell腳本與錯誤第一次運行,但正確地第二次或第三次
- 詢問用戶名
- 組「域用戶」基團如主組
- 在AD禁用用戶
- 移動到禁用OU
- 從AD明確管理器
- 刪除除「域用戶的所有組「
- 從exchande列表
添加disabled_mailboxes用戶
當我運行它一步一步,它做工精細也是從第一次
這裏是腳本:
$username = Read-Host -Prompt 'Enter Username'
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Import-Module ActiveDirectory
$user = Get-ADUser -Filter {(SamAccountName -eq $username)} -Properties MemberOf
#set "domain users" group as primary group
$group = get-adgroup "Domain Users" -properties @("primaryGroupToken")
get-aduser $username | set-aduser -replace @{primaryGroupID=$group.primaryGroupToken}
#disable users in AD
Get-ADUser -Filter {(SamAccountName -eq $username)} | Disable-ADAccount -ErrorAction SilentlyContinue
#move to disabled OU
Get-ADUser -Filter {(SamAccountName -eq $username)} | Move-ADObject –TargetPath 「OU=Users,OU=Disabled Objects,DC=xxxxxxx,DC=xxx,DC=XXX」
#clear Manager from AD
Get-ADUser -Filter {(SamAccountName -eq $username)} | Set-ADUser -Clear manager
#-------------------------
#remove all groups except 'domain users'
Get-ADPrincipalGroupMembership -Identity $username | % {Remove-ADPrincipalGroupMembership -Identity $username -MemberOf $_ -Confirm:$false -ErrorAction SilentlyContinue}
#code can be removed.
#$group = $user | Select-Object -ExpandProperty MemberOf
#Remove-ADGroupMember -Identity $group -Members $user.SamAccountName -Confirm:$false -ErrorAction SilentlyContinue
#-------------------------
#Add disabled_mailboxes to the user
Add-ADGroupMember -Identity 'disabled_mailboxes' -Member $User.SamAccountName -ErrorAction SilentlyContinue
#-------------------------
#Hide account from exchande list
Set-Mailbox -identity $user.SamAccountName -HiddenFromAddressListsEnabled $true -ErrorAction SilentlyContinue
的Windows 2012R2,Exchange 2010中
任何人都可以有幫助那???
感謝
米娜
看到類似的東西,我認爲這可能是複製問題,稍後再看,你能否將錯誤添加到問題中?確保ErrorAction設置爲「繼續」而不是「SilentlyContinue」,這將抑制錯誤 – SteloNLD