适用于 Windows 的 VMAccess 扩展

VMAccess 扩展用于管理用户、配置 RDP 以及检查或修复 Azure Windows 虚拟机上的磁盘。 该扩展集成了 Azure 资源管理器模板。 还可以使用 Azure CLI、Azure PowerShell、Azure 门户和 Azure 虚拟机 REST API 调用它。

本文介绍如何通过 Azure PowerShell 和 Azure 资源管理器模板运行 VMAccess 扩展。 本文还提供针对 Windows 系统的疑难解答步骤。

注释

如果你在安装 Microsoft Entra 登录扩展后使用 VMAccess 扩展重置 VM 的密码,请重新运行 Microsoft Entra 登录扩展,为 VM 重新启用 Azure AD 登录。

先决条件

支持的 Windows 版本

OS 版本。 x64 ARM64
Windows 10 支持 支持
Windows 11 支持 支持
Windows Server 2016 支持 支持
Windows Server 2016 核心版 支持 支持
Windows Server 2019 支持 支持
Windows Server 2019 核心版 支持 支持
Windows Server 2022 支持 支持
Windows Server 2022 核心版 支持 支持
Windows Server 2025 支持 支持
Windows Server 2025 核心版 支持 支持

提示

  • VMAccess 旨在当无法访问虚拟机 (VM) 时重新获取对该虚拟机的访问权限。 基于这一原则,它会将管理员权限授予用户名字段中指定的帐户。 如果你不希望某个用户拥有管理员权限,请登录到虚拟机并使用内置工具(如 net userLocal Users and Groups)来管理用户权限。
  • 只能向一台 VM 应用一个扩展版本。 要运行另一个操作,可使用新配置更新现有扩展。
  • 更新用户时,VMAccess 会修改远程桌面设置以允许登录。

扩展架构

VMAccess 扩展的配置包括用户名、密码以及重置管理员密码的相关设置。 可以将这些信息存储在配置文件中,在 PowerShell 中指定,或者包含在 Azure 资源管理器 (ARM) 模板中。 以下 JSON 架构包含可用于公共和受保护设置的所有属性。

{
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "name": "<name>",
  "apiVersion": "2023-09-01",
  "location": "<location>",
  "dependsOn": [
          "[concat('Microsoft.Compute/virtualMachines/', <vmName>)]"
  ],
  "properties": {
    "publisher": "Microsoft.Compute",
    "type": "VMAccessAgent",
    "typeHandlerVersion": "2.4",
    "autoUpgradeMinorVersion": true,
    "settings": {},
    "protectedSettings": {
      "username": "<username>",
      "password": "<password>",
      "reset_password": true
    } 
  }
}

属性值

名字 值/示例 数据类型
apiVersion 2023-09-01 日期
出版商 Microsoft.Compute 字符串
类型 VMAccessAgent 字符串
typeHandlerVersion 2.4 整数 (int)

设置属性值

名字 数据类型 DESCRIPTION
用户名 字符串 要管理的用户的名称(用户帐户上的所有操作都需要)。
密码 字符串 要为用户帐户设置的密码。
重置密码 布尔 是否重置用户密码。

使用 PowerShell 部署

可以使用 PowerShell 将 VMAccess 扩展应用到 Windows 虚拟机上。 以下 PowerShell 脚本是一个重置管理员密码的示例:

$resourceGroup = "<ResourceGroupName>"
$vmName = "<VMName>"
$location = "<Location>"
$username = "<Username>"
$password = "<NewPassword>"

$settings = @{}
$protectedSettings = @{
    "username" = $username
    "password" = $password
    "reset_password" = $true
}

Set-AzVMExtension -ResourceGroupName $resourceGroup `
                    -VMName $vmName `
                    -Location $location `
                    -Name "VMAccessAgent" `
                    -Publisher "Microsoft.Compute" `
                    -ExtensionType "VMAccessAgent" `
                    -TypeHandlerVersion "2.4" `
                    -Settings $settings `
                    -ProtectedSettings $protectedSettings

模板部署

可使用 Azure 资源管理器 (ARM) 模板部署 Azure VM 扩展。 上一部分中详细说明的 JSON 架构可用于 ARM 模板,以在模板部署期间运行 VMAccess 扩展。 可在 GitHub 上找到包含 VMAccess 扩展的示例模板。

必须将虚拟机扩展的 JSON 配置嵌套在该模板的虚拟机资源片段中,具体来说是嵌套在虚拟机模板的 "resources": [] 对象和 "virtualMachineProfile":"extensionProfile":{"extensions" :[] 对象下的虚拟机规模集。

使用适用于 Windows 的 Azure PowerShell VM/VMSS 扩展命令

可以使用 Set-AzVMExtension 命令,为 Windows 虚拟机按指定配置运行 VMAccess 扩展。

以下 PowerShell 脚本是使用 Set-AzVMExtension 命令的示例:

Set-AzVMExtension `
  -ResourceGroupName "myResourceGroup" `
  -VMName "myVM" `
  -Name "VMAccessAgent" `
  -Publisher "Microsoft.Compute" `
  -Type "VMAccessAgent" `
  -TypeHandlerVersion "2.0" `
  -Settings @{ "ResetPassword" = $true } `
  -ProtectedSettings @{ "Username" = "adminUser"; "Password" = "userPassword" }

-Settings 和 - ProtectedSettings 参数也接受 JSON 文件路径。 例如,要使用 JSON 文件更新本地管理员密码,请创建一个名为 update_admin_password.json 的文件,并包含以下内容。 将值替换为自己的信息:

{
  "Username": "adminUser",
  "Password": "userPassword"
}

然后,以该 JSON 文件作为输入运行扩展:

Set-AzVMExtension `
  -ResourceGroupName "myResourceGroup" `
  -VMName "myVM" `
  -Name "VMAccessAgent" `
  -Publisher "Microsoft.Compute" `
  -Type "VMAccessAgent" `
  -TypeHandlerVersion "2.0" `
  -ProtectedSettings (Get-Content -Path "update_admin_password.json" -Raw | ConvertFrom-Json)

适用于 Windows 的 Azure PowerShell 部署

可以使用 Azure PowerShell 将 VMAccess 扩展部署到现有的 Windows 虚拟机或虚拟机规模集上。 可以通过运行以下项将扩展部署到 VM:

$username = "<username>"
$password = "<password>"
$settings = @{ "ResetPassword" = $true }
$protectedSettings = @{ "Username" = $username; "Password" = $password }
Set-AzVMExtension -ResourceGroupName "<resource-group>" `
    -VMName "<vm-name>" `
    -Location "<location>" `
    -Publisher "Microsoft.Compute" `
    -ExtensionType "VMAccessAgent" `
    -Name "VMAccessAgent" `
    -TypeHandlerVersion "2.0" `
    -Settings $settings `
    -ProtectedSettings $protectedSettings

还可以使用字符串提供和修改扩展设置:

$username = "<username>"
$password = "<password>"
$settingsString = '{"ResetPassword": true}';
$protectedSettingsString = '{"Username":"' + $username + '", "Password":"' + $password + '"}';
Set-AzVMExtension -ResourceGroupName "<resource-group>" `
    -VMName "<vm-name>" `
    -Location "<location>" `
    -Publisher "Microsoft.Compute" `
    -ExtensionType "VMAccessAgent" `
    -Name "VMAccessAgent" `
    -TypeHandlerVersion "2.0" `
    -SettingString $settingsString `
    -ProtectedSettingString $protectedSettingsString

要部署到虚拟机规模集,请运行以下命令:

$resourceGroupName = "<resource-group>"
$vmssName = "<vmss-name>"
$protectedSettings = @{
  "Username" = "adminUser"
  "Password" = "userPassword"
}
$publicSettings = @{
  "ResetPassword" = $true
}
$vmss = Get-AzVmss `
            -ResourceGroupName $resourceGroupName `
            -VMScaleSetName $vmssName
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
    -Name "VMAccessAgent" `
    -Publisher "Microsoft.Compute" `
    -Type "VMAccessAgent" `
    -TypeHandlerVersion "2.0" `
    -AutoUpgradeMinorVersion $true `
    -Setting $publicSettings `
    -ProtectedSetting $protectedSettings
Update-AzVmss `
    -ResourceGroupName $resourceGroupName `
    -Name $vmssName `
    -VirtualMachineScaleSet $vmss

故障排除和支持

VMAccess 扩展的日志存储在虚拟机本地,对故障排除尤其有用。

位置 DESCRIPTION
C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.VMAccessAgent\ 包含来自 Windows 代理的日志,显示扩展何时进行了更新。 验证这些日志以确保扩展成功运行。
C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.VMAccessAgent\<version>\ VMAccess Extension 在此目录中生成详细日志。 其中包括 CommandExecution.log,它记录了执行的每个命令及其结果;以及 extension.log,其中包含单个执行日志。
C:\WindowsAzure\Logs\WaAppAgent\ 包含与 VMAccess 扩展相关的配置详细信息和二进制日志。

还可以运行以下命令来检索 VMAccess 扩展的执行状态以及给定 VM 上的其他扩展:

可以通过运行以下 PowerShell 命令来检索给定虚拟机上虚拟机扩展的执行状态:

Get-AzVMExtension -ResourceGroupName "myResourceGroup" -VMName "myVM" | Format-Table

错误消息

错误 DESCRIPTION
{“innererror”: {“internalErrorCode”: “无法在虚拟机未运行时修改扩展”}, “code”: “操作不允许”,“message”: “虚拟机未运行时无法修改扩展。”} 该错误表示不允许在虚拟机未运行时修改虚拟机中的扩展,因为虚拟机处于未运行状态。 请确保在尝试修改扩展之前,虚拟机处于运行状态。
虚拟机在处理扩展 "enablevmAccess"(发布者为 "Microsoft.Compute",类型为 "VMAccessAgent")时报告失败。 错误消息:“VMAccess 扩展不支持域控制器”。 有关故障排除的更多信息,请访问 https://aka.ms/vmextensionwindowstroubleshoot 该错误表示虚拟机扩展 "enablevmAccess" 失败,因为它不支持域控制器。 使用此扩展时,请确保虚拟机未配置为域控制器。 有关更多信息,请参阅在 Windows 虚拟机中重置远程桌面服务或其管理员密码
虚拟机 "vmname" 尚未报告虚拟机代理或扩展的状态。 请验证操作系统是否已启动且运行正常,虚拟机是否正在运行虚拟机代理,并且它是否可以建立到 Azure 存储的出站连接。 有关虚拟机代理故障排除的更多信息,请参阅 https://aka.ms/vmextensionwindowstroubleshoot 请参阅故障排除清单
虚拟机在处理扩展 "enablevmAccess"(发布者为 "Microsoft.Compute",类型为 "VMAccessAgent")时报告失败。 错误消息:“无法更新管理员帐户的远程桌面连接设置。 错误:System.Reflection.TargetInvocationException:调用的目标引发了异常。 ---> System.Runtime.InteropServices.COMException:密码不符合密码策略要求。 检查最低密码长度、密码复杂性和密码历史记录要求。 --- 内部异常堆栈跟踪结束 --- at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) at Microsoft.WindowsAzure.GuestAgent.Plugins.WindowsUser.SetPassword(SecureString password, Boolean expirePassword) at Microsoft.WindowsAzure.GuestAgent.Plugins.RemoteAccessAccountManager.AddOrUpdateRemoteUserAccount(String userName, SecureString password, Boolean expirePassword) at Microsoft.WindowsAzure.GuestAgent.Plugins.JsonExtensions.VMAccess.VMAccessExtension.OnEnable()'. 有关故障排除的更多信息,请访问https://aka.ms/vmextensionwindowstroubleshoot 该错误表示虚拟机扩展 "enablevmAccess" 由于违反密码策略,未能更新管理员帐户的远程桌面连接设置。 请确保密码符合 Windows 密码策略要求,包括最小长度、复杂性和历史记录要求。 有关更多信息,请参阅对虚拟机扩展进行故障排除
虚拟机在处理扩展 "enablevmAccess"(发布者为 "Microsoft.Compute",类型为 "VMAccessAgent")时报告失败。 错误消息:“如果提供了用户名,则管理员用户帐户密码不能为空或为空字符串。” 有关故障排除的更多信息,请访问 https://aka.ms/vmextensionwindowstroubleshoot 该错误表示虚拟机扩展 "enablevmAccess" 失败,因为未提供管理员用户帐户密码。 请确保为管理员用户帐户指定一个非空且不为空字符串的密码,以解决此问题。
虚拟机扩展 enablevmaccess 的预配已超时。完成扩展预配花费的时间过长。 扩展未报告任何消息。 该错误消息表示虚拟机扩展 "enablevmaccess" 的预配由于完成时间过长而超时。 此外,扩展在该过程中未提供任何状态消息。 要解决此问题,请考虑检查虚拟机的性能和网络状况,然后重试预配操作。 有关更多信息,请参阅 对 Azure Windows 虚拟机扩展故障进行故障排除
虚拟机在处理扩展 "enablevmAccess"(发布者为 "Microsoft.Compute",类型为 "VMAccessAgent")时报告失败。 错误消息:“无法更新管理员帐户的远程桌面连接设置。 错误:System.Exception:用户帐户 scsadmin 已存在,但无法更新,因为它不在管理员组中。 at Microsoft.WindowsAzure.GuestAgent.Plugins.RemoteAccessAccountManager.AddOrUpdateRemoteUserAccount(String userName, SecureString password, Boolean expirePassword) at Microsoft.WindowsAzure.GuestAgent.Plugins.JsonExtensions.VMAccess.VMAccessExtension.OnEnable(). 有关故障排除的更多信息,请访问 https://aka.ms/vmextensionwindowstroubleshoot 该错误表示虚拟机扩展 "enablevmAccess" 失败,因为用户帐户 "scsadmin" 已存在,但不在管理员组中。 请确保将该用户帐户添加到管理员组中以解决此问题。
虚拟机在处理扩展 "enablevmaccess"(发布者为 "Microsoft.Compute",类型为 "VMAccessAgent")时报告失败。 错误消息:“无法更新管理员帐户的远程桌面连接设置。 错误:System.Runtime.InteropServices.COMException (0x800708C5):密码不符合密码策略要求。 检查最低密码长度、密码复杂性和密码历史记录要求。 at System.DirectoryServices.DirectoryEntry.CommitChanges() at Microsoft.WindowsAzure.GuestAgent.Plugins.WindowsUser.SetPassword(SecureString password, Boolean expirePassword) at Microsoft.WindowsAzure.GuestAgent.Plugins.WindowsUserManager.CreateUserInGroup(String userName, SecureString password, Boolean expirePassword, String[] groups) at Microsoft.WindowsAzure.GuestAgent.Plugins.RemoteAccessAccountManager.AddOrUpdateRemoteUserAccount(String userName, SecureString password, Boolean expirePassword) at Microsoft.WindowsAzure.GuestAgent.Plugins.JsonExtensions.VMAccess.VMAccessExtension.OnEnable()'. 有关故障排除的更多信息,请访问 https://aka.ms/vmextensionwindowstroubleshoot 该错误消息表示虚拟机由于更新管理员帐户的远程桌面连接设置时出现问题,无法处理 "enablevmaccess" 扩展。 具体错误与密码不符合策略要求有关,例如最小长度、复杂性和历史记录要求。 要解决此问题,请确保密码符合所需的策略标准。 有关更多信息,请参阅对虚拟机扩展进行故障排除
{"innererror": {"internalErrorCode": "MultipleExtensionsPerHandlerNotAllowed"}, "code": "BadRequest","message": "操作系统类型「Windows」不支持每个处理程序使用多个 VM 扩展。"} VMExtension “enablevmaccess”,处理程序“Microsoft.Compute.VMAccessAgent”已在输入中添加或指定。“} 该错误消息表示对于 "Windows" 操作系统类型,不支持每个处理程序有多个虚拟机扩展。 处理程序为 "Microsoft.Compute.VMAccessAgent" 的虚拟机扩展 "enablevmaccess" 已添加或在输入中已指定。 要解决此问题,请确保为虚拟机配置每个处理程序仅一个扩展。

手动移除扩展并重试操作
Remove-AzVMExtension -ResourceGroupName "ResourceGroup11" -Name "ExtensionName" -VMName "VirtualMachineName"

如需更多帮助,可以提交 Azure 支持事件。 转到 Azure 门户,然后选择“获取支持”。 有关 Azure 支持的详细信息,请阅读 Azure 支持计划 FAQ