使用 PowerShell 启用 Microsoft Entra 域服务

Microsoft Entra 域服务提供与 Windows Server Active Directory 完全兼容的托管域服务,例如域加入、组策略、LDAP、Kerberos/NTLM 身份验证。 使用这些域服务就无需自行部署、管理和修补域控制器。 域服务可与你现有的 Microsoft Entra 租户集成。 这种集成可让用户使用其企业凭据登录,而你可以使用现有的组和用户帐户来保护对资源的访问。

本文介绍如何使用 PowerShell 启用域服务。

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

先决条件

若要完成本文,需准备好以下资源:

创建所需的 Microsoft Entra 资源

域服务要求使用服务主体进行身份验证和通信,并使用 Microsoft Entra 组定义哪些用户在托管域中具有管理权限。

首先,使用名为“域控制器服务”的特定应用程序 ID 创建 Microsoft Entra 服务主体。 对于全球 Azure,ID 值为 2565bd9d-da50-47d4-8b85-4c97f669dc36;对于其他 Azure 云,ID 值为 6ba9a5d4-8456-4118-b521-9c5ca10cdf84。 请不要更改此应用程序 ID。

使用 New-MgServicePrincipal cmdlet 创建 Microsoft Entra 服务主体:

New-MgServicePrincipal -AppId "2565bd9d-da50-47d4-8b85-4c97f669dc36"

现在,请创建名为“AAD DC 管理员”的 Microsoft Entra 组。 然后,添加到此组的用户会被授予在托管域上执行管理任务的权限。

首先,请使用 Get-MgGroup cmdlet 获取 AAD DC 管理员组对象 ID。 如果该组不存在,请使用 New-MgGroup cmdlet 通过 AAD DC 管理员组来创建它

# First, retrieve the object ID of the 'AAD DC Administrators' group.
$GroupObject = Get-MgGroup `
  -Filter "DisplayName eq 'AAD DC Administrators'"

# If the group doesn't exist, create it
if (!$GroupObject) {
  $GroupObject = New-MgGroup -DisplayName "AAD DC Administrators" `
    -Description "Delegated group to administer Microsoft Entra Domain Services" `
    -SecurityEnabled:$true `
    -MailEnabled:$false `
    -MailNickName "AADDCAdministrators"
  } else {
  Write-Output "Admin group already exists."
}

通过所创建的 AAD DC 管理员组,使用 Get-MgUser cmdlet 获取所需用户的对象 ID,然后使用 New-MgGroupMember cmdlet 将该用户添加到组中

以下示例显示了 UPN 为 admin@contoso.onmicrosoft.com 的帐户的用户对象 ID。 请将此用户帐户替换为要添加到“AAD DC 管理员”组的用户的 UPN:

# Retrieve the object ID of the user you'd like to add to the group.
$UserObjectId = Get-MgUser `
  -Filter "UserPrincipalName eq 'admin@contoso.onmicrosoft.com'" | `
  Select-Object Id

# Add the user to the 'AAD DC Administrators' group.
New-MgGroupMember -GroupId $GroupObject.Id -DirectoryObjectId $UserObjectId.Id

创建网络资源

首先,使用 Register-AzResourceProvider cmdlet 注册 Microsoft Entra 域服务资源提供程序:

Register-AzResourceProvider -ProviderNamespace Microsoft.AAD

接下来,使用 New-AzResourceGroup cmdlet 创建一个资源组。 以下示例中,资源组名为 myResourceGroup,在 westus 区域中创建。 使用自己的名称和所需区域:

$ResourceGroupName = "myResourceGroup"
$AzureLocation = "westus"

# Create the resource group.
New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $AzureLocation

为 Microsoft Entra 域服务创建虚拟网络和子网。 创建两个子网 - 一个用于“DomainServices”,另一个用于“Workloads”。 域服务将部署到专用的“DomainServices”子网中。 请不要将其他应用程序或工作负载部署到此子网中。 对剩余的 VM 使用单独的“Workloads”子网或其他子网。

使用 New-AzVirtualNetworkSubnetConfig cmdlet 创建子网,然后使用 New-AzVirtualNetwork cmdlet 创建虚拟网络。

$VnetName = "myVnet"

# Create the dedicated subnet for Microsoft Entra Domain Services.
$SubnetName = "DomainServices"
$AaddsSubnet = New-AzVirtualNetworkSubnetConfig `
  -Name $SubnetName `
  -AddressPrefix 10.0.0.0/24

# Create an additional subnet for your own VM workloads
$WorkloadSubnet = New-AzVirtualNetworkSubnetConfig `
  -Name Workloads `
  -AddressPrefix 10.0.1.0/24

# Create the virtual network in which you will enable Microsoft Entra Domain Services.
$Vnet= New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location westus `
  -Name $VnetName `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $AaddsSubnet,$WorkloadSubnet

创建网络安全组

域服务需要使用网络安全组来保护托管域所需的端口,阻止所有其他的传入流量。 网络安全组 (NSG) 包含一系列规则,这些规则可以允许或拒绝网络流量在 Azure 虚拟网络中流动。 在域服务中,网络安全组充当一个额外的保护层,以锁定对托管域的访问。 若要查看必需的端口,请参阅网络安全组和必需端口

以下 PowerShell cmdlet 使用 New-AzNetworkSecurityRuleConfig 创建规则,然后使用 New-AzNetworkSecurityGroup 创建网络安全组。 然后,使用 Set-AzVirtualNetworkSubnetConfig cmdlet 将网络安全组和规则与虚拟网络子网相关联。

$NSGName = "dsNSG"

# Create a rule to allow inbound TCP port 3389 traffic from Microsoft secure access workstations for troubleshooting
$nsg201 = New-AzNetworkSecurityRuleConfig -Name AllowRD `
    -Access Allow `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 201 `
    -SourceAddressPrefix CorpNetSaw `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389

# Create a rule to allow TCP port 5986 traffic for PowerShell remote management
$nsg301 = New-AzNetworkSecurityRuleConfig -Name AllowPSRemoting `
    -Access Allow `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 301 `
    -SourceAddressPrefix AzureActiveDirectoryDomainServices `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 5986

# Create the network security group and rules
$nsg = New-AzNetworkSecurityGroup -Name $NSGName `
    -ResourceGroupName $ResourceGroupName `
    -Location $AzureLocation `
    -SecurityRules $nsg201,$nsg301

# Get the existing virtual network resource objects and information
$vnet = Get-AzVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $SubnetName
$addressPrefix = $subnet.AddressPrefix

# Associate the network security group with the virtual network subnet
Set-AzVirtualNetworkSubnetConfig -Name $SubnetName `
    -VirtualNetwork $vnet `
    -AddressPrefix $addressPrefix `
    -NetworkSecurityGroup $nsg
$vnet | Set-AzVirtualNetwork

创建托管域

现在,让我们创建托管域。 设置 Azure 订阅 ID,然后提供托管域的名称,例如 dscontoso.com。 可以使用 Get-AzSubscription cmdlet 获取订阅 ID。

如果选择支持可用性区域的区域,则域服务资源会跨区域分布以实现冗余。

可用性区域是 Azure 区域中独特的物理位置。 每个区域由一个或多个数据中心组成,这些数据中心配置了独立电源、冷却和网络。 为确保能够进行复原,所有已启用的地区中都必须至少有三个单独的区域。

对于要跨区域分布域服务,无需进行任何配置。 Azure 平台会自动处理资源的区域分配。 有关详细信息和区域可用性,请参阅Azure 中的可用性区域是什么?

$AzureSubscriptionId = "YOUR_AZURE_SUBSCRIPTION_ID"
$ManagedDomainName = "dscontoso.com"

# Enable Microsoft Entra Domain Services for the directory.
$replicaSetParams = @{
  Location = $AzureLocation
  SubnetId = "/subscriptions/$AzureSubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Network/virtualNetworks/$VnetName/subnets/DomainServices"
}
$replicaSet = New-AzADDomainServiceReplicaSetObject @replicaSetParams

$domainServiceParams = @{
  Name = $ManagedDomainName
  ResourceGroupName = $ResourceGroupName
  DomainName = $ManagedDomainName
  ReplicaSet = $replicaSet
}
New-AzADDomainService @domainServiceParams

创建资源并将控制权返回给 PowerShell 提示符需要花费几分钟时间。 托管域将在后台继续预配,完成部署最长可能需要一小时。 在 Microsoft Entra 管理中心中,托管域的“概览”页会显示整个部署阶段的当前状态。

当 Microsoft Entra 管理中心显示托管域已完成预配时,需要完成以下任务:

  • 为虚拟网络更新 DNS 设置,以使虚拟机能够找到用于域加入或身份验证的托管域。
    • 若要配置 DNS,请在门户中选择你的托管域。 在“概览”窗口中,系统会提示你自动配置这些 DNS 设置。
  • 启用域服务的密码同步,使最终用户能够使用其企业凭据登录到托管域。

完整的 PowerShell 脚本

下述完整的 PowerShell 脚本结合了本文中所述的所有任务。 请复制该脚本,并将其保存到扩展名为 .ps1 的文件中。 对于 Azure 全球,使用 AppId 值 2565bd9d-da50-47d4-8b85-4c97f669dc36。 对于其他 Azure 云,使用 AppId 值 6ba9a5d4-8456-4118-b521-9c5ca10cdf84。 在本地 PowerShell 控制台或 Azure Cloud Shell 中运行该脚本。

注意

若要启用域服务,你必须是 Microsoft Entra 租户的全局管理员。 此外,该管理员需要在 Azure 订阅中至少拥有“参与者”特权。

# Change the following values to match your deployment.
$AaddsAdminUserUpn = "admin@contoso.onmicrosoft.com"
$ResourceGroupName = "myResourceGroup"
$VnetName = "myVnet"
$AzureLocation = "westus"
$AzureSubscriptionId = "YOUR_AZURE_SUBSCRIPTION_ID"
$ManagedDomainName = "dscontoso.com"

# Connect to your Microsoft Entra directory.
Connect-MgGraph -Scopes "Application.ReadWrite.All","Directory.ReadWrite.All"

# Login to your Azure subscription.
Connect-AzAccount

# Create the service principal for Microsoft Entra Domain Services.
New-MgServicePrincipal -AppId "2565bd9d-da50-47d4-8b85-4c97f669dc36"

# First, retrieve the object of the 'AAD DC Administrators' group.
$GroupObject = Get-MgGroup `
  -Filter "DisplayName eq 'AAD DC Administrators'"

# Create the delegated administration group for Microsoft Entra Domain Services if it doesn't already exist.
if (!$GroupObject) {
  $GroupObject = New-MgGroup -DisplayName "AAD DC Administrators" `
    -Description "Delegated group to administer Microsoft Entra Domain Services" `
    -SecurityEnabled:$true `
    -MailEnabled:$false `
    -MailNickName "AADDCAdministrators"
  } else {
  Write-Output "Admin group already exists."
}

# Now, retrieve the object ID of the user you'd like to add to the group.
$UserObjectId = Get-MgUser `
  -Filter "UserPrincipalName eq '$AaddsAdminUserUpn'" | `
  Select-Object Id

# Add the user to the 'AAD DC Administrators' group.
New-MgGroupMember -GroupId $GroupObject.Id -DirectoryObjectId $UserObjectId.Id

# Register the resource provider for Microsoft Entra Domain Services with Resource Manager.
Register-AzResourceProvider -ProviderNamespace Microsoft.AAD

# Create the resource group.
New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $AzureLocation

# Create the dedicated subnet for Microsoft Entra Domain Services.
$SubnetName = "DomainServices"
$AaddsSubnet = New-AzVirtualNetworkSubnetConfig `
  -Name DomainServices `
  -AddressPrefix 10.0.0.0/24

$WorkloadSubnet = New-AzVirtualNetworkSubnetConfig `
  -Name Workloads `
  -AddressPrefix 10.0.1.0/24

# Create the virtual network in which you will enable Microsoft Entra Domain Services.
$Vnet=New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $AzureLocation `
  -Name $VnetName `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $AaddsSubnet,$WorkloadSubnet

$NSGName = "dsNSG"

# Create a rule to allow inbound TCP port 3389 traffic from Microsoft secure access workstations for troubleshooting
$nsg201 = New-AzNetworkSecurityRuleConfig -Name AllowRD `
    -Access Allow `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 201 `
    -SourceAddressPrefix CorpNetSaw `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 3389

# Create a rule to allow TCP port 5986 traffic for PowerShell remote management
$nsg301 = New-AzNetworkSecurityRuleConfig -Name AllowPSRemoting `
    -Access Allow `
    -Protocol Tcp `
    -Direction Inbound `
    -Priority 301 `
    -SourceAddressPrefix AzureActiveDirectoryDomainServices `
    -SourcePortRange * `
    -DestinationAddressPrefix * `
    -DestinationPortRange 5986

# Create the network security group and rules
$nsg = New-AzNetworkSecurityGroup -Name $NSGName `
    -ResourceGroupName $ResourceGroupName `
    -Location $AzureLocation `
    -SecurityRules $nsg201,$nsg301

# Get the existing virtual network resource objects and information
$vnet = Get-AzVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name $SubnetName
$addressPrefix = $subnet.AddressPrefix

# Associate the network security group with the virtual network subnet
Set-AzVirtualNetworkSubnetConfig -Name $SubnetName `
    -VirtualNetwork $vnet `
    -AddressPrefix $addressPrefix `
    -NetworkSecurityGroup $nsg
$vnet | Set-AzVirtualNetwork

# Enable Microsoft Entra Domain Services for the directory.
$replicaSetParams = @{
  Location = $AzureLocation
  SubnetId = "/subscriptions/$AzureSubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Network/virtualNetworks/$VnetName/subnets/DomainServices"
}
$replicaSet = New-AzADDomainServiceReplicaSet @replicaSetParams

$domainServiceParams = @{
  Name = $ManagedDomainName
  ResourceGroupName = $ResourceGroupName
  DomainName = $ManagedDomainName
  ReplicaSet = $replicaSet
}
New-AzADDomainService @domainServiceParams

创建资源并将控制权返回给 PowerShell 提示符需要花费几分钟时间。 托管域将在后台继续预配,完成部署最长可能需要一小时。 在 Microsoft Entra 管理中心中,托管域的“概览”页会显示整个部署阶段的当前状态。

当 Microsoft Entra 管理中心显示托管域已完成预配时,需要完成以下任务:

  • 为虚拟网络更新 DNS 设置,以使虚拟机能够找到用于域加入或身份验证的托管域。
    • 若要配置 DNS,请在门户中选择你的托管域。 在“概览”窗口中,系统会提示你自动配置这些 DNS 设置。
  • 启用域服务的密码同步,使最终用户能够使用其企业凭据登录到托管域。

后续步骤

若要查看托管域的运作方式,可将某个 Windows VM 加入域配置安全 LDAP,并配置密码哈希同步