分享方式:


使用 Microsoft Graph PowerShell 設定從 Microsoft Entra 識別碼到 Microsoft Entra Domain Services 的範圍同步處理

為了提供驗證服務,Microsoft Entra Domain Services 會同步處理來自 Microsoft Entra ID 的使用者和群組。 在混合式環境中,您可以從 內部部署的 Active Directory Domain Services (AD DS) 環境的使用者和群組,先使用 Microsoft Entra 連線 同步處理至 Microsoft Entra ID,然後同步處理至 Domain Services。

根據預設,Microsoft Entra 目錄中的所有使用者和群組都會同步處理至 Domain Services 受控網域。 如果您有特定需求,您可以改為選擇只同步處理一組已定義的使用者。

本文說明如何建立使用範圍同步處理的受控網域,然後使用 MS Graph PowerShell 變更或停用一組限定範圍的使用者。 您也可以 使用 Microsoft Entra 系統管理中心來完成這些步驟。

開始之前

若要完成本文章,您需要下列資源和權限:

限定範圍的同步處理概觀

根據預設,來自 Microsoft Entra 目錄的所有使用者和群組都會同步處理至受控網域。 如果只有少數使用者需要存取受控網域,您就只能同步處理這些用戶帳戶。 此限定範圍的同步處理是以群組為基礎。 當您設定以群組為基礎的範圍同步處理時,只有屬於您指定群組的用戶帳戶會同步處理至受控網域。 巢狀群組不會同步處理,只有您選取的特定群組。

您可以在建立受控網域之前或之後變更同步處理範圍。 同步處理的範圍是由應用程式標識碼為 2565bd9d-da50-47d4-8b85-4c97f669dc36 的服務主體所定義。 若要防止範圍遺失,請勿刪除或變更服務主體。 如果意外刪除,則無法復原同步處理範圍。

如果您變更同步處理範圍,請記住下列注意事項:

  • 發生完整同步處理。
  • 已刪除受控網域中不再需要的物件。 新的物件會在受控網域中建立。

若要深入瞭解同步處理程式,請參閱 瞭解 Microsoft Entra Domain Services 中的同步處理

範圍同步處理的PowerShell腳本

若要使用PowerShell設定範圍同步處理,請先將下列腳本儲存至名為的 Select-GroupsToSync.ps1檔案。

此腳本會將 Domain Services 設定為從 Microsoft Entra ID 同步選取的群組。 屬於指定群組的所有用戶帳戶都會同步處理至受控網域。

本文的其他步驟會使用此腳本。

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

Connect-MgGraph -Scopes "Directory.Read.All","AppRoleAssignment.ReadWrite.All"
$sp = Get-MgServicePrincipal -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-MgGroup -Filter "DisplayName eq '$groupName'"
        $newGroupIds.Add($group.Id)

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

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

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

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

    if ($newGroupIds.Contains($assignment.PrincipalId) -eq $true)
    {
        Write-Output "This assignment is not needed anymore. Removing it! Assignment-ObjectId: $($assignment.PrincipalId)"
        Remove-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $sp.Id -AppRoleAssignmentId $assignment.Id
    }
    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.Id)"
            $appRoleAssignment = @{
                "principalId"= $group.Id
                "resourceId"= $sp.Id
                "appRoleId"= $role.Id
            }
            New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $sp.Id -BodyParameter $appRoleAssignment 
        }
        else
        {
            Write-Output "Group-ObjectId: $id is already assigned."
        }
    }
    catch
    {
        Write-Error "Exception occurred assigning Object-ID: $id. Exception: $($_.Exception)."
    }
}

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

啟用限定範圍的同步處理

若要啟用受控網域的群組型範圍同步處理,請完成下列步驟:

  1. 請先在 Domain Services 資源上設定 「filteredSync」 = “Enabled” ,然後更新受控網域。 出現提示時,請指定 Global 管理員 istrator 的認證,以使用 連線-MgGraph Cmdlet 登入您的 Microsoft Entra 租使用者:

    # Connect to your Entra ID tenant
    Connect-MgGraph -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
    
  2. 現在,指定使用者應該同步處理至受控網域的群組清單。

    Select-GroupsToSync.ps1執行文稿,並指定要同步的群組清單。在下列範例中,要同步處理的群組為 GroupName1GroupName2

    警告

    您必須在範圍同步處理的群組清單中包含 AAD DC 管理員 istrators 群組。 如果您沒有包含此群組,則受控網域無法使用。

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

變更同步處理範圍會導致受控網域重新同步處理所有數據。 已刪除受控網域中不再需要的物件,且重新同步處理可能需要很長的時間才能完成。

修改限定範圍的同步處理

若要修改使用者應該同步至受控網域的群組清單,請執行 Select-GroupsToSync.ps1 腳本並指定要同步處理的新群組清單。

在下列範例中,要同步處理的群組不再包含 GroupName2,現在包含 GroupName3

警告

您必須在範圍同步處理的群組清單中包含 AAD DC 管理員 istrators 群組。 如果您沒有包含此群組,則受控網域無法使用。

出現提示時,請指定 Global 管理員 istrator 的認證,以使用 連線-MgGraph Cmdlet 登入您的 Microsoft Entra 租使用者

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

變更同步處理範圍會導致受控網域重新同步處理所有數據。 已刪除受控網域中不再需要的物件,且重新同步處理可能需要很長的時間才能完成。

停用限定範圍的同步處理

若要停用受控網域的群組型範圍同步處理,請在 Domain Services 資源上設定 “filteredSync” = “Disabled” ,然後更新受控網域。 完成時,所有使用者和群組都會設定為從 Microsoft Entra ID 同步處理。

出現提示時,請指定 Global 管理員 istrator 的認證,以使用 連線-MgGraph Cmdlet 登入您的 Microsoft Entra 租使用者:

# Connect to your Entra ID tenant
Connect-MgGraph -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 Domain Services 中的同步處理