使用 Azure CLI 中的磁盘快照创建 Windows VM

问题描述 投票:0回答:1

有没有办法使用 Azure CLI 从磁盘快照创建 Windows VM?出奇。有很多很棒的文档,但我在尝试期间遇到了不同的错误,例如

Use of securityProfile.securityType setting is not supported for attached OS disk.
Security type of VM is not compatible with the security type of attached OS Disk.

windows azure virtual-machine azure-cli snapshot
1个回答
0
投票

准备工作

您需要下载最新版本的azure-cli客户端。
链接在这里:https://aka.ms/installazurecliwindows

代码

以下代码对我有帮助。
根据您的需求指定参数!

REM The Subscription ID can be seen on any Azure resource in the "Essentials" section.
set subscriptionId=<YOUR SUBSCRIPTION ID>
set resourceGroupName=<YOUR RESOURCE GROUP NAME>

REM Defining some common and disk-related parameters
set diskName=<TYPE A DISK NAME>
set zoneId=1
set snapshotName=<TYPE YOUR SNAPSHOT NAME>
set osType=windows
set secType=TrustedLaunch
set diskVmGeneration=V2
set diskArchitecture=x64
set diskSize=128
set diskStorageType=StandardSSD_LRS

REM Defining some VM-related parameters
set virtualMachineName=<TYPE A VM NAME>
set vmSize=Standard_D2s_v3
set licenseType=Windows_Server
set vnetName=<TYPE A VNET NAME>
set publicIpAddressAllocation=static
set publicIpAddressSku=Standard
set isSecureBootEnabled=true
set isVtpmEnabled=true


REM Logging in
az login

REM Selecting the subscription
az account set --subscription %subscriptionId%

REM Getting the snapshot Id - This step is commented out because the snapshot name can be added to the disk creation script
REM for /f %i in ('az snapshot show --name %snapshotName% --resource-group %resourceGroupName% --query [id] -o tsv') do set snapshotId=%i

REM Creating a new managed disk
az disk create --resource-group %resourceGroupName% ^
               --name %diskName% ^
               --zone %zoneId% ^
               --source %snapshotName% ^
               --os-type %osType% ^
               --hyper-v-generation %diskVmGeneration% ^
               --architecture %diskArchitecture% ^
               --size-gb %diskSize% ^
               --sku %diskStorageType%

REM Creating vnet
az network vnet create --name %vnetName% --resource-group %resourceGroupName%

REM Creating a new VM using created managed disk
az vm create --resource-group %resourceGroupName% ^
             --name %virtualMachineName% ^
             --zone %zoneId% ^
             --security-type %secType% ^
             --attach-os-disk %diskName% ^
             --os-type %osType% ^
             --size %vmSize% ^
             --license-type %licenseType% ^
             --vnet-name %vnetName% ^
             --public-ip-address-allocation %publicIpAddressAllocation% ^
             --public-ip-sku %publicIpAddressSku% ^
             --enable-secure-boot %isSecureBootEnabled% ^
             --enable-vtpm %isVtpmEnabled%

有用的信息

磁盘创建参数
https://learn.microsoft.com/en-us/cli/azure/disk?view=azure-cli-latest#az-disk-create

Vnet 创建参数
https://learn.microsoft.com/en-us/cli/azure/network/vnet?view=azure-cli-latest#az-network-vnet-create

虚拟机创建参数
https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az-vm-create

© www.soinside.com 2019 - 2024. All rights reserved.