共用方式為


使用 PowerShell 啟用 Microsoft Entra Domain Services

Microsoft Entra Domain Services 提供受控網域服務,例如與 Windows Server Active Directory 完全相容的網域加入、群組原則、LDAP、Kerberos/NTLM 驗證。 您不需要自行部署、管理和修補網域控制站,就可以使用這些網域服務。 Domain Services 會與您的現有 Microsoft Entra 租用戶整合。 這項整合可讓使用者使用其公司認證進行登入,而您可以使用現有的群組和使用者帳戶來保護資源的存取。

本文說明如何使用 PowerShell 啟用 Domain Services。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

若要完成本文,您需要下列資源:

  • 安裝並設定 Azure PowerShell。

  • 安裝和設定 MS Graph PowerShell。

  • 您必須擁有 Microsoft Entra 租用戶的全域管理員權限,才能啟用 Domain Services。

  • 您需要 Azure 訂用帳戶中的參與者權限,才能建立必要的 Domain Services 資源。

    重要

    Az.ADDomainServices PowerShell 模組處於預覽狀態,因此您必須使用 Install-Module Cmdlet 將其分開安裝。

    Install-Module -Name Az.ADDomainServices
    

建立必要的 Microsoft Entra 資源

Domain Services 需要服務主體進行驗證和通訊,以及需要 Microsoft Entra 群組以定義哪些使用者在受控網域中具有系統管理權限。

首先,使用名為網域控制站服務的特定應用程式識別碼來建立 Microsoft Entra 服務主體。 全域 Azure 的識別碼值為 2565bd9d-da50-47d4-8b85-4c97f669dc36,其他 Azure 雲端的識別碼值為 6ba9a5d4-8456-4118-b521-9c5ca10cdf84。 請勿變更此應用程式識別碼。

使用 New-MgServicePrincipal Cmdlet 來建立 Microsoft Entra 服務主體:

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

現在,建立名為 AAD DC 管理員的 Microsoft Entra 群組。 系統會授與新增至此群組的使用者權限,以在受控網域上執行管理工作。

首先,使用 Get-MgGroup Cmdlet,取得 AAD DC 管理員群組物件識別碼。 如果群組不存在,則使用 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 取得想要的使用者物件識別碼,然後使用 New-MgGroupMember Cmdlet 將使用者新增至群組。

在下列範例中,帳戶的使用者物件識別碼為 admin@contoso.onmicrosoft.com 的 UPN。 將此使用者帳戶取代為您要新增至 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 Domain Services 資源提供者:

Register-AzResourceProvider -ProviderNamespace Microsoft.AAD

接下來,使用 New-AzResourceGroup Cmdelt 建立新的資源群組。 在下列範例中,會在 westus 區域中建立名為 myResourceGroup 的資源群組。 使用您自己的名稱和所需的區域:

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

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

建立適用於 Microsoft Entra Domain Services 的虛擬網路及子網路。 已建立兩個子網路,一個用於 DomainServices,另一個用於 工作負載。 Domain Services 會部署到專用的 DomainServices 子網路中。 請勿將其他應用程式或工作負載部署到這個子網路。 針對其餘的 VM,使用個別的工作負載 或其他子網路。

使用 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

建立網路安全性群組

Domain Services 需要一個網路安全性群組來保護受控網域所需的連接埠,並封鎖所有其他的傳入流量。 網路安全性群組 (NSG) 包含規則清單,可允許或拒絕 Azure 虛擬網路中的網路流量。 在 Domain Services 中,網路安全性群組可作為額外的保護層,以鎖定對受控網域的存取。 若要檢視所需的連接埠,請參閱網路安全性群組與必要連接埠

下列 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 訂用帳戶識別碼,然後提供受控網域的名稱,例如 dscontoso.com。 您可以使用 Get-AzSubscriptionn Cmdlet 取得訂用帳戶識別碼。

如果您選擇支援可用性區域的地區,則 Domain Services 資源會跨區域分佈以實現備援。

「可用性區域」是 Azure 地區內獨特的實體位置。 每個區域都是由一或多個資料中心所組成,配備了獨立的電力、冷卻系統及網路系統。 若要確保復原能力,在所有已啟用的區域中都至少要有三個個別的區域。

您不需要設定,Domain Services 就會跨區域分散。 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 設定。
  • 啟用 Domain Services 的密碼同步化,讓使用者可使用他們的公司認證來登入受控網域。

完整的 PowerShell 指令碼

下列完整的 PowerShell 指令碼結合了本文中顯示的所有工作。 複製指令碼,並儲存至使用 .ps1 副檔名的檔案。 針對 Azure Global,搜尋 AppId 值 2565bd9d-da50-47d4-8b85-4c97f669dc36。 針對其他 Azure 雲端,使用 AppId 值 6ba9a5d4-8456-4118-b521-9c5ca10cdf84。 在本機 PowerShell 主控台或 Azure Cloud Shell 中執行指令碼。

注意

若要啟用 Domain Services,您必須是 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 設定。
  • 啟用 Domain Services 的密碼同步化,讓使用者可使用他們的公司認證來登入受控網域。

下一步

若要查看作用中的受控網域,您可以將 Windows VM 加入網域設定安全 LDAP,以及設定密碼雜湊同步處理