使用 MS Graph PowerShell 配置从 Microsoft Entra ID 到 Microsoft Entra 域服务的区分范围的同步

为了提供身份验证服务,Microsoft Entra 域服务从 Microsoft Entra ID 同步用户和组。 在混合环境中,本地 Active Directory 域服务 (AD DS) 环境中的用户和组可以先使用 Microsoft Entra Connect 同步到 Microsoft Entra ID,然后再同步到域服务。

默认情况下,Microsoft Entra 目录中的所有用户和组都同步到域服务托管域。 如果有特定需求,可以改为选择只同步所定义的一组用户。

本文说明如何创建使用作用域同步的托管域,然后使用 MS Graph PowerShell 更改或禁用作用域用户的集合。 还可以使用 Microsoft Entra 管理中心完成这些步骤

准备阶段

需有以下资源和特权才能完成本文:

具有作用域的同步概述

默认情况下,Microsoft Entra 目录中的所有用户和组都会同步到托管域。 如果只有几个用户需要访问托管域,则可以仅同步这些用户帐户。 此具有作用域的同步基于组。 配置基于组的具有作用域的同步时,只有属于指定组的用户帐户才会同步到托管域。 不同步嵌套组,只同步所选的特定组。

可以在创建托管域之前或之后更改同步范围。 同步范围由应用程序标识符为 2565bd9d-da50-47d4-8b85-4c97f669dc36 的服务主体定义。 为了防止范围丢失,请不要删除或更改服务主体。 如果意外删除了服务主体,则无法恢复同步范围。

如果更改同步范围,请记住以下注意事项:

  • 进行完全同步。
  • 托管域中不再需要的对象会被删除。 托管域中会新建对象。

若要详细了解同步过程,请参阅了解 Microsoft Entra 域服务中的同步

用于具有作用域的同步的 PowerShell 脚本

若要使用 PowerShell 配置具有作用域的同步,请首先将以下脚本保存到名为 Select-GroupsToSync.ps1 的文件中。

此脚本将域服务配置为从 Microsoft Entra ID 同步所选组。 属于指定组的所有用户帐户都会同步到托管域。

此脚本将用于本文中的其他步骤。

param (
    [Parameter(Position = 0)]
    [String[]]$groupsToAdd
)

Connect-AzureAD -AzureEnvironmentName AzureChinaCloud
$sp = Get-AzureADServicePrincipal -Filter "AppId eq '2565bd9d-da50-47d4-8b85-4c97f669dc36'"
$role = $sp.AppRoles | where-object -FilterScript {$_.DisplayName -eq "User"}

Write-Output "`n****************************************************************************"

Write-Output "Total group-assignments need to be added: $($groupsToAdd.Count)"
$newGroupIds = New-Object 'System.Collections.Generic.HashSet[string]'
foreach ($groupName in $groupsToAdd)
{
    try
    {
        $group = Get-AzureADGroup -Filter "DisplayName eq '$groupName'"
        $newGroupIds.Add($group.ObjectId)

        Write-Output "Group-Name: $groupName, Id: $($group.ObjectId)"
    }
    catch
    {
        Write-Error "Failed to find group: $groupName. Exception: $($_.Exception)."
    }
}

Write-Output "****************************************************************************`n"
Write-Output "`n****************************************************************************"

$currentAssignments = Get-AzureADServiceAppRoleAssignment -ObjectId $sp.ObjectId -All $true
Write-Output "Total current group-assignments: $($currentAssignments.Count), SP-ObjectId: $($sp.ObjectId)"

$currAssignedObjectIds = New-Object 'System.Collections.Generic.HashSet[string]'
foreach ($assignment in $currentAssignments)
{
    Write-Output "Assignment-ObjectId: $($assignment.PrincipalId)"

    if ($newGroupIds.Contains($assignment.PrincipalId) -eq $false)
    {
        Write-Output "This assignment is not needed anymore. Removing it! Assignment-ObjectId: $($assignment.PrincipalId)"
        Remove-AzureADServiceAppRoleAssignment -ObjectId $sp.ObjectId -AppRoleAssignmentId $assignment.ObjectId
    }
    else
    {
        $currAssignedObjectIds.Add($assignment.PrincipalId)
    }
}

Write-Output "****************************************************************************`n"
Write-Output "`n****************************************************************************"

foreach ($id in $newGroupIds)
{
    try
    {
        if ($currAssignedObjectIds.Contains($id) -eq $false)
        {
            Write-Output "Adding new group-assignment. Role-Id: $($role.Id), Group-Object-Id: $id, ResourceId: $($sp.ObjectId)"
            New-AzureADGroupAppRoleAssignment -Id $role.Id -ObjectId $id -PrincipalId $id -ResourceId $sp.ObjectId
        }
        else
        {
            Write-Output "Group-ObjectId: $id is already assigned."
        }
    }
    catch
    {
        Write-Error "Exception occurred assigning Object-ID: $id. Exception: $($_.Exception)."
    }
}

Write-Output "****************************************************************************`n"

启用作用域同步

若要为托管域启用基于组的作用域同步,请完成以下步骤:

  1. 首先在域服务资源上设置 "filteredSync" = "Enabled",然后更新托管域。 出现提示时,使用 Connect-MgGraph cmdlet 为全局管理员指定用于登录到 Microsoft Entra 租户的凭据:
    # Connect to your Entra ID tenant
    Connect-MgGraph -Environment China -Scopes "Application.ReadWrite.All","Group.ReadWrite.All"

    # Retrieve the Microsoft Entra DS resource.
    $DomainServicesResource = Get-AzResource -ResourceType "Microsoft.AAD/DomainServices"

    # Enable group-based scoped synchronization.
    $enableScopedSync = @{"filteredSync" = "Enabled"}

    # Update the Microsoft Entra DS resource
    Set-AzResource -Id $DomainServicesResource.ResourceId -Properties $enableScopedSync
    ```

1. Now specify the list of groups whose users should be synchronized to the managed domain.

    Run the `Select-GroupsToSync.ps1` script and specify the list of groups to sync. In the following example, the groups to synchronize are *GroupName1* and *GroupName2*.

    > [!WARNING]
    > You must include the *AAD DC Administrators* group in the list of groups for scoped synchronization. If you don't include this group, the managed domain is unusable.

    ```powershell
    .\Select-GroupsToSync.ps1 -groupsToAdd @("AAD DC Administrators", "GroupName1", "GroupName2")
    ```

Changing the scope of synchronization causes the managed domain to resynchronize all data. Objects that are no longer required in the managed domain are deleted, and resynchronization may take a long time to complete.

## Modify scoped synchronization

To modify the list of groups whose users should be synchronized to the managed domain, run `Select-GroupsToSync.ps1` script and specify the new list of groups to sync.

In the following example, the groups to synchronize no longer includes *GroupName2*, and now includes *GroupName3*.

> [!WARNING]
> You must include the *AAD DC Administrators* group in the list of groups for scoped synchronization. If you don't include this group, the managed domain is unusable.

When prompted, specify the credentials for a *global admin* to sign in to your Microsoft Entra tenant using the [Connect-MgGraph](https://learn.microsoft.com/powershell/microsoftgraph/authentication-commands?view=graph-powershell-1.0) cmdlet:

```powershell
.\Select-GroupsToSync.ps1 -groupsToAdd @("AAD DC Administrators", "GroupName1", "GroupName3")

更改同步作用域会导致托管域重新同步所有数据。 托管域中不再需要的对象会被删除,重新同步可能需要很长时间才能完成。

禁用具有作用域的同步

若要为托管域禁用基于组的具有作用域的同步,请在域服务资源上设置 "filteredSync" = "Disabled",然后更新托管域。 完成后,所有用户和组都设置为从 Microsoft Entra ID 进行同步。

出现提示时,使用 Connect-MgGraph cmdlet 为全局管理员指定用于登录到 Microsoft Entra 租户的凭据:

# Connect to your Entra ID tenant
Connect-MgGraph -Environment China -Scopes "Application.ReadWrite.All","Group.ReadWrite.All"

# Retrieve the Microsoft Entra DS resource.
$DomainServicesResource = Get-AzResource -ResourceType "Microsoft.AAD/DomainServices"

# Disable group-based scoped synchronization.
$disableScopedSync = @{"filteredSync" = "Disabled"}

# Update the Microsoft Entra DS resource
Set-AzResource -Id $DomainServicesResource.ResourceId -Properties $disableScopedSync

更改同步作用域会导致托管域重新同步所有数据。 托管域中不再需要的对象会被删除,重新同步可能需要很长时间才能完成。

后续步骤

若要详细了解同步过程,请参阅了解 Microsoft Entra 域服务中的同步

Connect-MgGraph: /powershell/microsoftgraph/authentication-commands?view=graph-powershell-1.0