本页介绍了如何使用 Managed Service for Microsoft Active Directory 中的自动域名加入功能将 Windows Compute Engine 虚拟机实例加入网域。
托管式 Microsoft AD 如何自动将 Windows 虚拟机加入网域
如需使用托管式 Microsoft AD 对虚拟机上运行的应用进行身份验证,您需要将虚拟机加入托管式 Microsoft AD 网域。域名加入流程通常需要执行一些手动步骤。
创建或更新 Windows Compute Engine 虚拟机时,您可以使用脚本自动执行手动方法,将虚拟机加入托管式 Microsoft AD 网域。不过,如需在 Compute Engine 虚拟机上执行这些脚本,您需要 AD 凭据(需要安全地存储和维护),以及用于预配和运行这些脚本的环境。为了避免使用凭据和额外的服务,您可以使用 Managed Microsoft AD 提供的现成脚本自动执行域名加入流程。
创建 Compute Engine 虚拟机时,您可以使用脚本将虚拟机自动加入托管式 Microsoft AD 网域。Compute Engine 创建虚拟机后,代管式 Microsoft AD 会发起域名加入请求,并尝试将虚拟机加入您的网域。如果网域加入请求成功,托管式 Microsoft AD 会将创建的虚拟机加入您的网域。如果加入网域请求失败,则创建的虚拟机会继续运行。出于安全或结算目的,您可以自定义此行为,这样,当网域加入请求失败时,托管式 Microsoft AD 可以停止虚拟机。
更新 Compute Engine 虚拟机时,您可以使用脚本自动将现有虚拟机加入到托管式 Microsoft AD 网域。为了让网域加入请求成功,Managed Microsoft AD 会在运行脚本后重启虚拟机。
使用此元数据键指定要加入的代管式 Microsoft AD 网域的完整资源名称,格式为:projects/PROJECT_ID/locations/global/domains/DOMAIN_NAME。例如 projects/my-project-123/locations/global/domains/my-domain.example.com。
managed-ad-domain-join-failure-stop
可选:默认情况下,即使域名加入请求失败,虚拟机也会继续运行。如果您希望在请求失败时停止虚拟机,可以将此元数据键设置为 TRUE。在您设置此元数据键后,受管理的 Microsoft AD 可以停止虚拟机,但不会删除虚拟机。
可选:默认情况下,托管式 Microsoft AD 会将虚拟机加入到 Cloud 组织部门下预先创建的 GCE Instances 组织部门 (OU),以便更好地管理政策。如需详细了解 Cloud OU,请参阅组织部门。
如果您想将虚拟机加入自定义 OU,则需要在托管式 Microsoft AD 的 GCE Instances OU 或 Cloud OU 下创建自定义 OU,并使用此元数据键指定自定义 OU。托管式 Microsoft AD 不支持您在 Cloud OU 或 GCE Instances OU 以外的任何位置创建的自定义 OU。
如果您在 Cloud OU 下创建自定义 OU,请使用以下格式指定自定义 OU 的路径:/cloud/SUB_OU1/SUB_OU2/…/CUSTOM_OU。例如 /cloud/my-sub-ou/my-custom-ou。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-11。"],[],[],null,["# Join a Windows VM automatically to a domain\n\nThis page explains how to join a Windows Compute Engine VM instance to a domain using the automated domain join feature in Managed Service for Microsoft Active Directory.\n\nHow Managed Microsoft AD joins a Windows VM automatically to a domain\n---------------------------------------------------------------------\n\nTo use Managed Microsoft AD for authenticating the applications running on your VMs, you need to join the VMs to your Managed Microsoft AD domain. The domain join process usually involves performing some manual steps.\n\nWhen you create or update a Windows Compute Engine VM, you can join the VM to your Managed Microsoft AD domain by automating [the manual approach](/managed-microsoft-ad/docs/quickstart-domain-join-windows) using scripts. However, to execute these scripts on a Compute Engine VM, you require AD credentials that need to be securely stored and maintained, and an environment to provision and run these scripts. To eliminate the need for credentials and an additional service, you can automate the domain join process with ready-made scripts that are available from Managed Microsoft AD.\n\nWhen you create Compute Engine VMs, you can use scripts to automatically join the VMs to your Managed Microsoft AD domain. After Compute Engine creates the VMs, Managed Microsoft AD initiates the domain join request and attempts to join the VMs with your domain. If the domain join request succeeds, Managed Microsoft AD joins the created VMs to your domain. If the domain join request fails, the created VMs continue to run. For security or billing purposes, you can customize this behavior and Managed Microsoft AD can stop the VMs when the domain join request fails.\n\nWhen you update Compute Engine VMs, you can use scripts to automatically join the existing VMs to your Managed Microsoft AD domain. For the domain join request to succeed, Managed Microsoft AD restarts the VMs after running the scripts.\n\nBefore you begin\n----------------\n\n1. [Create a Managed Microsoft AD domain](/managed-microsoft-ad/docs/create-domain).\n\n2. Make sure that the VM name has a maximum of 15 characters.\n\n3. Make sure that the VM runs on a [Windows version that Managed Microsoft AD supports](/managed-microsoft-ad/docs/os-versions#windows-domain-join).\n\n4. [Configure domain peering](/managed-microsoft-ad/docs/quickstart-domain-peering) between the Managed Microsoft AD domain and the VM's network, or have both the Managed Microsoft AD domain and the VM in the same network.\n\n5. Create a service account with the Google Cloud Managed Identities Domain Join (`roles/managedidentities.domainJoin`) IAM role on the project that has the Managed Microsoft AD domain. For more information, see [Cloud Managed Identities roles](/iam/docs/understanding-roles#cloud-managed-identities-roles).\n\n - For more information about granting roles, see [Grant a single role](/iam/docs/manage-access-service-accounts#grant-single-role).\n\n - For information about creating a service account, see [Authenticate workloads using service accounts](/compute/docs/access/create-enable-service-accounts-for-instances).\n\n6. Set the full `cloud-platform` access scope on the VM. For more information, see [Authorization](/compute/docs/access/service-accounts#authorization).\n\nMetadata\n--------\n\nYou need the following metadata keys to join a Windows VM to a domain.\n\nJoin the Windows VM\n-------------------\n\nYou can use these [metadata keys](/managed-microsoft-ad/docs/seamless-domain-join-gce#metadata) when you either create a Windows VM or update an existing VM.\nThe following sections illustrate how to use these metadata keys in gcloud CLI commands when you create a VM or update a VM.\n\nHowever, you can use these metadata keys with a VM using the other available options as well. For more information about using metadata with a Windows Compute Engine VM, see [Set custom metadata](/compute/docs/metadata/setting-custom-metadata).\n\n### Join a Windows VM during creation\n\nTo create and join a Windows Compute Engine VM, run the following gcloud CLI command: \n\n```\ngcloud compute instances create INSTANCE_NAME \\\n --metadata=windows-startup-script-url=URL,managed-ad-domain=DOMAIN_RESOURCE_PATH,managed-ad-domain-join-failure-stop=TRUE,enable-guest-attributes=TRUE \\\n --service-account=SERVICE_ACCOUNT \\\n --scopes=https://www.googleapis.com/auth/cloud-platform \\\n --image-project windows-cloud \\\n --image-family IMAGE_FAMILY\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e: Name of the Windows Compute Engine VM to create. For example, `my-instance-1`.\n- \u003cvar translate=\"no\"\u003eURL\u003c/var\u003e: A publicly-accessible location of the Windows startup script that the VM executes during the startup process.\n- \u003cvar translate=\"no\"\u003eDOMAIN_RESOURCE_PATH\u003c/var\u003e: Full resource name of your Managed Microsoft AD domain to join. For example, `projects/my-project-123/locations/global/domains/my-domain.example.com`.\n- \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT\u003c/var\u003e: A service account that you want to attach to the VM. For example, `my-sa-123@my-project-123.iam.gserviceaccount.com`.\n- `--scopes`: The default access scopes configured in the VM restricts the domain join request. You need to set the full `cloud-platform` access scope on the VM. For more information, see [Authorization](/compute/docs/access/service-accounts#authorization).\n- `--image-project`: You need to set this flag as `windows-cloud` to create a Windows VM. For more information, see [`gcloud compute instances create`](/sdk/gcloud/reference/compute/instances/create).\n- \u003cvar translate=\"no\"\u003eIMAGE_FAMILY\u003c/var\u003e: Specify one of the [public image families](/compute/docs/images#os-compute-support) that has images for the [supported Windows versions](/managed-microsoft-ad/docs/os-versions#windows-domain-join). For example, `windows-2019-core`.\n\nFor more information about adding metadata during VM creation, see [Set metadata during VM creation](/compute/docs/metadata/setting-custom-metadata#set_during_creation).\n\n### Join an existing Windows VM\n\nYou can update the [metadata keys](/managed-microsoft-ad/docs/seamless-domain-join-gce#metadata) on an existing Windows Compute Engine VM and join the VM to your domain. After you add these metadata keys to the VM, restart the VM so that the domain join request succeeds.\n| **Caution:** If you already have a startup script that is passed to the VM using the `windows-startup-script-url` metadata key, the following approach can replace the existing startup script with the automated domain join startup script. Instead, you can use any other suitable [metadata key](/compute/docs/instances/startup-scripts/windows#metadata-keys) to pass the existing startup script, except the `windows-startup-script-url` metadata key. For example, `windows-startup-script-ps1`. For information about the script execution sequence, see [Order of execution of Windows startup scripts](/compute/docs/instances/startup-scripts/windows#order_of_execution_of_windows_startup_scripts).\n\nTo join an existing Windows Compute Engine VM, run the following gcloud CLI command: \n\n```\ngcloud compute instances add-metadata INSTANCE_NAME \\\n --metadata=windows-startup-script-url=URL,managed-ad-domain=DOMAIN_RESOURCE_PATH,managed-ad-domain-join-failure-stop=TRUE,enable-guest-attributes=TRUE \\\n --service-account=SERVICE_ACCOUNT \\\n --scopes=https://www.googleapis.com/auth/cloud-platform\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e: Name of the Windows Compute Engine VM that you want to join. For example, `my-instance-1`.\n- \u003cvar translate=\"no\"\u003eURL\u003c/var\u003e: A publicly-accessible location of the Windows startup script that the VM executes after restart.\n- \u003cvar translate=\"no\"\u003eDOMAIN_RESOURCE_PATH\u003c/var\u003e: Full resource name of your Managed Microsoft AD domain to join. For example, `projects/my-project-123/locations/global/domains/my-domain.example.com`.\n- \u003cvar translate=\"no\"\u003eSERVICE_ACCOUNT\u003c/var\u003e: The service account that you have attached the VM during creation. For example, `my-sa-123@my-project-123.iam.gserviceaccount.com`.\n- `--scopes`: The default access scopes configured in the VM restricts the domain join request. You need to set the full `cloud-platform` access scope on the VM. For more information, see [Authorization](/compute/docs/access/service-accounts#authorization).\n\nFor more information about adding metadata to an existing VM, see [Updating metadata on a running VM](/compute/docs/metadata/setting-custom-metadata#update_metadata).\n\nClean up unjoined VMs\n---------------------\n\nWe recommend to delete the computer account manually from Managed Microsoft AD in the following scenarios:\n\n- If you delete a VM that you have joined with the Managed Microsoft AD domain.\n- If a VM has failed to join with the Managed Microsoft AD domain.\n\nView debug logs\n---------------\n\nIf the domain join request fails, you can check the logs for the startup script to identify and troubleshoot the issue. To check the logs for the startup script, you can [view Serial port 1 output](/compute/docs/troubleshooting/viewing-serial-port-output#viewing_serial_port_output). If you have enabled guest attributes on the VM, you can [get the guest attributes](/compute/docs/metadata/manage-guest-attributes#get_guest_attributes) to view the logs.\n\nFor information about the common errors that you can encounter while joining a VM to a domain, see [Unable to join a Windows VM automatically to a domain](/managed-microsoft-ad/docs/troubleshooting#domain-join-windows).\n\nWhat's next\n-----------\n\n- [Join GKE Windows Server nodes automatically to a\n Managed Microsoft AD domain](/managed-microsoft-ad/docs/automated-domain-join-gke)."]]