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

[玩转系统] 如何使用 PowerShell 将 Office 365 组成员身份复制给其他用户?

作者:精品下载站 日期:2024-12-15 00:26:31 浏览:16 分类:玩电脑

如何使用 PowerShell 将 Office 365 组成员身份复制给其他用户?


要求:将组成员身份复制给 Office 365 中的其他用户。

如何将 O365 组成员资格从一个用户复制到另一个用户?

如果您正在管理 Office 365 并且需要将组成员资格从一个用户复制到另一个用户,则没有简单的方法可以做到这一点!您可能需要在各种情况下执行此操作,例如当新员工入职并且必须具有与现有员工相同的访问权限时,或者当员工离开公司并且其职责必须重新分配给其他人时。让我引导您完成将一个用户的组成员身份复制到另一个用户所需的步骤。

  1. 登录 Microsoft 365 管理中心 https://admin.microsoft.com/
  2. 单击组 >> 活动组。在用户属性面板中搜索并选择源用户>>,记下该用户所属的所有组。

    [玩转系统] 如何使用 PowerShell 将 Office 365 组成员身份复制给其他用户?

  3. 现在,从您从源用户记下的组列表中选择目标用户 >> 将成员身份分配给目标用户。

当你有很多组时,这个手动过程有多痛苦?如果我们使用 PowerShell 脚本自动执行此过程,该脚本将读出源用户的成员资格,然后将新用户添加到同一组中,会怎么样?

用于复制 Office 365 组成员资格的 PowerShell

如果要在 Microsoft 365 环境中克隆用户的组成员身份,请使用此 PowerShell 脚本。在执行此脚本之前,请确保已安装 Azure AD 模块。


#Parameters
$SourceUserAccount = "[email protected]"
$TargetUserAccount = "[email protected]"

#Connect to Azure AD
Connect-AzureAD

#Get the Source and Target users
$SourceUser = Get-AzureADUser -Filter "UserPrincipalName eq '$SourceUserAccount'"
$TargetUser = Get-AzureADUser -Filter "UserPrincipalName eq '$TargetUserAccount'"

#Check if source and Target users are valid
If($SourceUser -ne $Null -and $TargetUser -ne $Null)
{
    #Get All memberships of the Source user
    $SourceMemberships = Get-AzureADUserMembership -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" }

    #Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId

    #Loop through Each Group
    ForEach($Membership in $SourceMemberships)
    {
        #Check if the user is not part of the group
        $GroupMembers = (Get-AzureADGroupMember -ObjectId $Membership.Objectid).UserPrincipalName
        If ($GroupMembers -notcontains $TargetUserAccount)
        {
            #Add Target user to the Source User's group
            Add-AzureADGroupMember -ObjectId $Membership.ObjectId -RefObjectId $TargetUser.ObjectId
            Write-host "Added user to Group:" $Membership.DisplayName
        }
    }
}
Else
{
    Write-host "Source or Target user is invalid!" -f Yellow
}

请注意,此脚本获取所有组成员身份,包括 Microsoft 365 组/统一组、安全组、通讯组列表以及给定用户添加为组成员的启用邮件的安全组。要获取用户是所有者的所有组,请使用以下命令:


$Ownerships  = Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" 

使用 PowerShell 复制 Office 365 组所有权

以下是用于复制用户组所有权的 PowerShell 脚本:


#Parameters - UPN
$SourceUserAccount = "[email protected]"
$TargetUserAccount = "[email protected]"

#Connect to Azure AD
Connect-AzureAD

#Get the Source and Target users
$SourceUser = Get-AzureADUser -Filter "UserPrincipalName eq '$SourceUserAccount'"
$TargetUser = Get-AzureADUser -Filter "UserPrincipalName eq '$TargetUserAccount'"

#Check if source and Target users are valid
If($SourceUser -ne $Null -and $TargetUser -ne $Null)
{
    #Get All Groups where the Source user is a Owner
    $SourceOwnerships = Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" }

    #Loop through Each Group
    ForEach($Ownership in $SourceOwnerships)
    {
        #Check if the user is not part of the group
        $GroupOwners = (Get-AzureADGroupOwner -ObjectId $Ownership.Objectid).UserPrincipalName
        If ($GroupOwners -notcontains $TargetUserAccount)
        {
            #Add Target user to the Source User's group
            Add-AzureADGroupOwner -ObjectId $Ownership.ObjectId -RefObjectId $TargetUser.ObjectId
            Write-host "Added user to Group:" $Ownership.DisplayName
        }
    }
}
Else
{
    Write-host "Source or Target user is invalid!" -f Yellow
}

使用 PowerShell 复制组成员身份可以节省大量时间和精力,因为它允许您快速授予用户与其他用户相同的所有资源和权限的访问权限。通过遵循本教程中概述的步骤,您可以快速授予用户与其他用户相同的所有资源和权限的访问权限。与往常一样,最好仔细检查目标用户的成员身份,以确保已分配正确的权限。

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

取消回复欢迎 发表评论:

关灯