繁体   English   中英

Powershell 用于从通讯组中删除用户的脚本

[英]Powershell script to remove users from distribution groups

我正在尝试找到一种解决方案,将用户从他们所在的所有通讯组中删除。我找到了这个脚本,但遇到了问题:

$email = Read-Host "Please provide a user's email address to remove from all distribution groups"
$mailbox = Get-Mailbox -Identity $email
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
$DistributionGroupsList = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter
Write-host `n
Write-host "Listing all Distribution Groups:"
Write-host `n
$DistributionGroupsList | ft
$answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?" 
While ("y","n" -notcontains $answer) {
    $answer = Read-Host "Would you like to proceed and remove $email from all distribution groups ( y / n )?"
    }
If ($answer -eq 'y') {
    ForEach ($item in $DistributionGroupsList) {
        Remove-DistributionGroupMember -Identity $item.DisplayName –Member $email –BypassSecurityGroupManagerCheck -Confirm:$false
    }
    
    Write-host `n
    Write-host "Successfully removed"
    Remove-Variable * -ErrorAction SilentlyContinue
    }
Else
    {
    Remove-Variable * -ErrorAction SilentlyContinue
    }

它将进入列出用户所在的所有组并询问是否删除它们的阶段,但它似乎卡在 –BypassSecurityGroupManagerCheck 上,提示此参数存在问题。

我在 Microsoft TechCenter 论坛上找到了这篇文章……显然您需要扩展它以满足您的需求。 - https://social.technet.microsoft.com/Forums/exchange/en-US/99c1f07b-12fa-4e06-95bd-246a757bb00f/powershellscript-to-remove-all-group-memberships-for-one-user

$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySmtpAddress}) -contains "user@domain.com"}
 
foreach( $dg in $DGs){
Remove-DistributionGroupMember $dg -Member user@domain.com
}

根据对事实的评论 - 不一样,您可以在 PowerShell ISE 或 VisualStudio 代码中打开脚本,然后在您写出每个命令的旁边,您可以按键盘上的 Tab 键,如果参数与该命令一起使用。 但是是的,我会根据你最后的评论说

无法执行该操作,因为在“DCWESTBROM01.accord.local”上找不到 object“rebecca.edge@greensqaureaccord.co.uk”。 + CategoryInfo: NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException + FullyQualifiedErrorId: [Server=EXCH2016-01,RequestId=ae3c7f93-e204-4245-aa5f-8678ff68aa63,[FailureCategory=Cmdlet-ManagementObjectNotFoundException] 4EA0476A,Microsoft.Exchange。 Management.RecipientTasks.GetMailbox

email 不存在。 我测试了用正确的破折号替换所有破折号的脚本(最好在记事本中完成以避免格式化)并且对我来说似乎没有错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM