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

Posted

技术标签:

【中文标题】用于从通讯组中删除用户的 Powershell 脚本【英文标题】:Powershell script to remove users from distribution groups 【发布时间】:2021-12-30 07:05:26 【问题描述】:

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

$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 上,提示此参数存在问题。

【问题讨论】:

"通知此参数存在问题。" - 是否建议该名称的参数不存在?也许发布完整的错误消息? @MathiasR.Jessen 这是错误:找不到与参数名称“BypassSecurityGroupManagerCheck”匹配的参数。 + CategoryInfo : InvalidArgument: (:) [Remove-DistributionGroupMember], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Remove-DistributionGroupMember + PSComputerName : exch2016-01.accord.local 所以删除它:) @MathiasR.Jessen 看起来怎么样?已经尝试删除它,但我只是在它的位置收到更多错误。 ... 错误说明了什么?请注意,没有其他人可以看到您的屏幕,“更多错误”不会告诉我们任何信息 :) 【参考方案1】:

我在 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 脚本的主要内容,如果未能解决你的问题,请参考以下文章

用于从 Appfabric 缓存中删除项目的 Powershell 命令

powershell Powershell脚本,用于从已编译的twine游戏中删除保存加密代码。

使用 Powershell 或 C# 从 TFS 项目中删除用户

Azure Devops 使用 Powershell 从特定组织中删除特定用户

如何使用 PowerShell 从 MSMQ 消息队列中删除特定消息

删除整个森林通讯组中的用户