当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 将所有者添加到通讯组

作者:精品下载站 日期:2024-12-14 05:51:20 浏览:15 分类:玩电脑

如何使用 PowerShell 将所有者添加到通讯组


管理员需要对通讯组进行许多更改。当涉及到管理多个通讯组所有者时,这可能是一项艰巨的任务。在本文中,您将了解如何使用 PowerShell 添加、删除和替换通讯组列表组所有者。

连接到 Exchange Online PowerShell

为了能够运行 PowerShell 命令,您必须连接到 Exchange Online PowerShell。以管理员身份打开 Windows PowerShell,运行以下 cmdlet,然后使用管理员凭据登录。

Connect-ExchangeOnline

Set-DistributionGroup 错误:您没有足够的权限

每个通讯组至少有一个所有者。这意味着只有通讯组所有者才被允许执行这些管理任务。因此,当 Exchange Online 管理员尝试使用 Set-DistributionGroup cmdlet 更新通讯组的属性,并且它不是该通讯组的所有者时,会显示以下错误消息。

错误:您没有足够的权限。该操作只能由群组管理员执行。

请注意,即使具有全局管理员凭据的用户也无法删除或更改通讯组。您不是通讯组所有者这一事实可能会阻止您完成所需的管理任务。

管理通讯组所需的 PowerShell cmdlet 包含名为 -BypassSecurityGroupManagerCheck 的参数。因此,每当您尝试执行管理任务并出现权限错误时,您都可以将-BypassSecurityGroupManagerCheck参数添加到原始PowerShell命令中。

请参阅 PowerShell 命令语法示例。

Set-DistributionGroup -Identity "Distribution Group" -ManagedBy "Identity" -BypassSecurityGroupManagerCheck

运行 PowerShell 命令示例。

Set-DistributionGroup -Identity "[email protected]" -ManagedBy "[email protected]" -BypassSecurityGroupManagerCheck

在我们的示例中,管理员([email protected])可以控制和管理通讯组(Sales UK)

通讯组 - 管理者参数

-ManagedBy 参数指定组的所有者。一个组必须至少有一个所有者。如果创建群组时不使用该参数指定所有者,则默认创建该群组的用户帐户为所有者。

[玩转系统] 如何使用 PowerShell 将所有者添加到通讯组

通讯组所有者可以:

  • 修改组属性
  • 添加或删除群组成员
  • 删除群组
  • 批准会员离开或加入请求(如果有)
  • 如果启用了审核但未指定审核者,则批准发送到群组的消息

所有组必须至少有一名所有者,而通讯组可以有多个所有者。

设置通讯组所有者

让我们看看如何使用 PowerShell 将通讯组所有者添加到通讯组列表或启用邮件的安全组。

在单个通讯组中添加其他所有者

您可以使用 PowerShell 将其他所有者添加到您的通讯组列表组。

在我们的示例中,通讯组的所有者(美国销售)是用户(David Kent)。我们将另外两个现有用户作为所有者添加到通讯组列表中。

请参阅 PowerShell 命令语法。

Set-DistributionGroup "Distribution Group" -ManagedBy @{Add='Identity 1','Identity 2'} -BypassSecurityGroupManagerCheck

运行以下 PowerShell 命令示例。

Set-DistributionGroup "[email protected]" -ManagedBy @{Add='[email protected]','[email protected]'} -BypassSecurityGroupManagerCheck

在单个通讯组中添加其他所有者并删除现有所有者

假设我们有一个由几个所有者组成的分发组。我们希望实现以下要求:

  • 添加新所有者
  • 删除一些现有的通讯组所有者

注意:您无法删除所有通讯组所有者。每个通讯组应该始终有一个所有者。

请参阅 PowerShell 命令语法。

Set-DistributionGroup "Distribution Group" -ManagedBy @{Add='Identity 1','Identity 2';Remove='Identity 1'} -BypassSecurityGroupManagerCheck

运行以下 PowerShell 命令示例。

Set-DistributionGroup "[email protected]" -ManagedBy @{Add='[email protected]','[email protected]';Remove='[email protected]'} -BypassSecurityGroupManagerCheck

在我们的示例中,我们添加了两个新所有者并删除了 Sales USA 的通讯组所有者。

从 CSV 将其他所有者添加到多个通讯组

如果您想将其他所有者添加到多个通讯组,最好使用 CSV 文件。它将把用户作为组所有者添加到通讯组列表中,但不会删除任何以前的所有者。

您需要创建一个包含两列的 CSV 文件。

  • GroupName:存储有关通讯组信息的列标题
  • 管理者:存储有关我们希望定义为通讯组所有者的用户名的信息的列标题

请按照以下步骤创建 CSV 文件以替换现有所有者:

  1. 打开Microsoft Excel
  2. 在第一列顶部输入GroupName
  3. 列出通讯组主 SMTP 地址
  4. 在第二列顶部键入 ManagedBy
  5. 列出新通讯组所有者UserPrincipalName

[玩转系统] 如何使用 PowerShell 将所有者添加到通讯组

将文件命名为 DL Group Owner,并将其作为 CSV 文件保存在 temp 文件夹中(如果 (C:) 中还没有该文件)驱动。

  1. 为了确保 PowerShell 可以读取该文件,请运行 Import-Csv cmdlet
Import-Csv "C:\temp\DL Group Owner.csv"
  1. 运行以下 PowerShell 脚本
# Specify the path to your CSV file
$csvPath = "C:\temp\DL Group Owner.csv"

# Import the CSV file
$csvData = Import-Csv -Path $csvPath

# Loop through each row in the CSV file and update the distribution group
foreach ($row in $csvData) {
    $groupName = $row.GroupName
    $owner = $row.ManagedBy

    # Check if the user specified in Owner exists
    $userExists = @()
    $userExists += Get-User -Identity $owner -ErrorAction SilentlyContinue
    $userExists += Get-Contact -Identity $owner -ErrorAction SilentlyContinue

    # Check if the distribution group specified in GroupName exists
    $groupExists = Get-DistributionGroup -Identity $groupName -ErrorAction SilentlyContinue

    if ($userExists -and $groupExists) {
        # Both user and group exist, so proceed with updating the distribution group
        try {
            # Get the user's ID
            $userID = $userExists.Identity

            # Check if the user is already an owner of the group
            $isOwner = Get-DistributionGroup -Identity $groupName | Where-Object { $_.ManagedBy -contains $userID }

            if (-not $isOwner) {
                # User is not an owner, so add them
                Set-DistributionGroup -Identity $groupName -ManagedBy @{Add = $userID } -BypassSecurityGroupManagerCheck -ErrorAction Stop
                Write-Host "Added user $owner to $groupName as owner." -ForegroundColor Green
            }
            else {
                Write-Host "User $owner is already an owner of $groupName. No action needed." -ForegroundColor Cyan
            }
        }
        catch {
            Write-Host "Failed to update owner for $groupName. Error: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    elseif (-not $groupExists) {
        # Group doesn't exist, display a message
        Write-Host "Distribution group $groupName specified in GroupName column doesn't exist. Skipping." -ForegroundColor Yellow
    }
    elseif (-not $userExists) {
        # User doesn't exist, display a message
        Write-Host "User $owner specified in owner column doesn't exist. Skipping." -ForegroundColor Yellow
    }
}

PowerShell输出结果显示:

  • 当您添加用户作为通讯组的所有者时
  • 如果通讯组不存在
  • 如果用户已经是通讯组的所有者
  • 用户在 Microsoft 365 中不存在
Added user [email protected] to [email protected] as owner.
Added user [email protected] to [email protected] as owner.
User [email protected] is already an owner of [email protected]. No action needed.
User [email protected] specified in owner column doesn't exist. Skipping.
User [email protected] is already an owner of [email protected]. No action needed.
User [email protected] specified in owner column doesn't exist. Skipping.
Added user [email protected] to [email protected] as owner.
User [email protected] is already an owner of [email protected]. No action needed.

替换通讯组所有者

要替换通讯组所有者,您需要分配一个新所有者,这将自动删除旧的通讯组所有者。

替换单个分发组中的现有所有者

您始终可以更改通讯组列表或启用邮件的安全组的所有者。

在我们的示例中,通讯组 Sales USA 拥有多个所有者。我们希望将所有者更改为单一所有者,[email protected]

注意:假设通讯组已存在多个所有者,并且您将其替换为单个所有者,则所有现有所有者都将被删除。

请参阅 PowerShell 命令语法。

Set-DistributionGroup -Identity "Distribution Group" -ManagedBy "Identity" -BypassSecurityGroupManagerCheck

运行以下 PowerShell 命令示例。

Set-DistributionGroup -Identity "[email protected]" -ManagedBy "[email protected]" -BypassSecurityGroupManagerCheck

通讯组 Sales USA 的新所有者已被 [email protected] 取代。

为过去两周内创建的所有通讯组分配新所有者

我们希望将用户 ([email protected]) 指定为过去 2 周内创建的所有现有通讯组的通讯组所有者。

PowerShell 命令将由以下部分组成:

  1. 在第一部分中,我们定义 PowerShell 变量 $AllDistributionGroups。它将存储 PowerShell 查询的输出并获取在特定时间创建的所有通讯组。
  2. 为了定义两周的时间范围,我们需要添加 14 天的时间范围。
  3. 我们将使用 ForEach PowerShell 语句。对结果执行循环过程,将Brenda Smith添加为列表中显示的每个现有(安全)通讯组的所有者。
  4. 如果您不是通讯组的所有者,则无法执行管理任务。因此,将 -BypassSecurityGroupManagerCheck 参数添加到原始 PowerShell 命令中。

运行以下 PowerShell 命令。

$AllDistributionGroups = Get-DistributionGroup -ResultSize Unlimited | Where-Object { $_.WhenCreated -ge (Get-Date).AddDays(-14) }

foreach ($Group in $AllDistributionGroups) {
    Set-DistributionGroup -Identity $Group.Name -ManagedBy "[email protected]" -BypassSecurityGroupManagerCheck
}

我们指定布伦达·史密斯 (Brenda Smith) 为过去两周内创建的所有通讯组的唯一所有者。

批量替换所有现有通讯组的所有者

我们想要将特定用户定义为所有现有通讯组的所有者。因此,我们将删除现有的通讯组所有者,并为所有通讯组分配新的所有者。

[玩转系统] 如何使用 PowerShell 将所有者添加到通讯组

  • PowerShell 命令的第一部分获取每个现有分发和启用邮件的安全组的列表。
  • PowerShell 命令的第二部分会将特定用户作为所有者添加到您从第一部分获得的通讯组列表中。

我们希望所有现有通讯组都由特定所有者 ([email protected]) 替换。

请参阅下面的 PowerShell 命令语法。

Get-DistributionGroup -ResultSize Unlimited | Set-DistributionGroup -ManagedBy "Identity" -BypassSecurityGroupManagerCheck

运行以下 PowerShell 命令。

Get-DistributionGroup -ResultSize Unlimited | Set-DistributionGroup -ManagedBy "[email protected]" -BypassSecurityGroupManagerCheck

就是这样!

了解更多:使用 PowerShell 将 Azure AD 组成员导出到 CSV »

结论

您了解了如何使用 PowerShell 管理通讯组所有者。使用正确的命令,您可以添加、删除和替换通讯组的所有者。如果您不是通讯组的所有者,请使用-BypassSecurityGroupManagerCheck 参数来管理所有通讯组设置。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 批量创建共享邮箱。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯