通过 CLI 使用现有托管 OS 磁盘创建虚拟机Create a virtual machine using an existing managed OS disk with CLI

此脚本通过将现有托管磁盘附加为 OS 磁盘来创建虚拟机。This script creates a virtual machine by attaching an existing managed disk as OS disk. 在前面的方案中使用此脚本:Use this script in preceding scenarios:

  • 基于从不同订阅中的托管磁盘复制的现有托管 OS 磁盘创建 VMCreate a VM from an existing managed OS disk that was copied from a managed disk in different subscription
  • 基于从专用 VHD 文件创建的现有托管磁盘创建 VMCreate a VM from an existing managed disk that was created from a specialized VHD file
  • 基于从快照创建的现有托管 OS 磁盘创建 VMCreate a VM from an existing managed OS disk that was created from a snapshot

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,请在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

示例脚本Sample script

#Provide the subscription Id
subscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0

#Provide the name of your resource group
resourceGroupName=myResourceGroupName

#Provide the name of the Managed Disk
managedDiskName=myDiskName

#Provide the OS type
osType=linux

#Provide the name of the virtual machine
virtualMachineName=myVirtualMachineName123

#Set the context to the subscription Id where Managed Disk exists and where VM will be created
az account set --subscription $subscriptionId

#Get the resource Id of the managed disk
managedDiskId=$(az disk show --name $managedDiskName --resource-group $resourceGroupName --query [id] -o tsv)

#Create VM by attaching existing managed disks as OS
az vm create --name $virtualMachineName --resource-group $resourceGroupName --attach-os-disk $managedDiskId --os-type $osType

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源。Run the following command to remove the resource group, VM, and all related resources.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令获取托管磁盘属性,将托管磁盘附加到新 VM 并创建 VM。This script uses the following commands to get managed disk properties, attach a managed disk to a new VM and create a VM. 表中的每一项均链接到特定于命令的文档。Each item in the table links to command specific documentation.

CommandCommand 说明Notes
az disk showaz disk show 使用磁盘名称和资源组名称获取托管磁盘属性。Gets managed disk properties using disk name and resource group name. Id 属性用来将托管磁盘附加到新 VMId property is used to attach a managed disk to a new VM
az vm createaz vm create 使用托管 OS 磁盘创建 VMCreates a VM using a managed OS disk

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.