使用 PowerShell 啟用 Microsoft Entra Domain Services

Microsoft Entra Domain Services 提供受控網域服務,例如網域加入、組策略、LDAP、Kerberos/NTLM 驗證,與 Windows Server Active Directory 完全相容。 您不需要自行部署、管理和修補網域控制站,就可以使用這些網域服務。 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 群組,以定義哪些使用者具有受控網域中的系統管理許可權。

首先,使用名為 Domain Controller Services 的特定應用程式識別碼,建立 Microsoft Entra 服務主體。 標識符值是 全域 Azure 的 2565bd9d-da50-47d4-8b85-4c97f669dc36而 6ba9a5d4-8456-4118-b521-9c5ca10cdf84 適用於其他 Azure 雲端。 請勿變更此應用程式識別碼。

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

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

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

首先,使用 Get-MgGroup Cmdlet 取得 AAD DC 管理員 istrators 群組對象識別碼。 如果群組不存在,請使用 New-MgGroup Cmdlet 以 AAD DC 管理員 istrators 群組建立:

# 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 管理員 istrators 群組後,使用 Get-MgUser Cmdlet 取得所需的使用者物件識別碼,然後使用 New-MgGroupMember Cmdlet 將使用者新增至群組

在下列範例中,具有UPN admin@contoso.onmicrosoft.com之帳戶的用戶物件標識碼。 將此使用者帳戶取代為您想要新增至 AAD DC 管理員 istrators 群組的使用者 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 Cmdlet 建立資源群組。 在下列範例中,會在 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 虛擬網路中流量流量的規則清單。 在網域服務中,網路安全組會作為額外的保護層,以鎖定受控網域的存取權。 若要檢視所需的埠,請參閱 網路安全組和必要的埠

下列 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-AzSubscription Cmdlet 取得訂用帳戶識別碼。

如果您選擇支援 可用性區域 的區域,網域服務資源會分散到區域以進行備援。

「可用性區域」是 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 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 設定。
  • 啟用網域服務 的密碼同步處理,讓使用者可以使用其公司認證登入受控網域。

下一步

若要查看受控網域的運作情形,您可以 加入 Windows VM設定安全 LDAP,以及 設定密碼哈希同步