共用方式為


使用 CLI 在相同訂用帳戶的記憶體帳戶中,從 VHD 檔案建立受控磁碟 (Linux)

此腳本會從相同訂用帳戶中記憶體帳戶中的 VHD 檔案建立受控磁碟。 使用此腳本將特製化(非一般化/sysprepped)VHD 匯入受控OS磁碟,以建立虛擬機。 或者,使用它將資料 VHD 匯入受管理的資料磁碟。

若要執行此範例,請安裝最新版的 Azure CLI。 若要啟動,請執行 az login 來建立與 Azure 的連線。

Azure CLI 的範例專為 bash 殼層撰寫。 若要在 Windows PowerShell 或命令提示字元中執行此範例,您可能需要變更指令碼的元素。

如果您沒有 Azure 訂用帳戶,請在開始之前,先建立 Azure 免費帳戶

範例腳本

# Verified per Raman Kumar as of 2/23/2022

# <FullScript>
#Provide the subscription Id
subscriptionId="<subscriptionId>"

#Provide the name of your resource group.
#Ensure that resource group is already created 
resourceGroupName=myResourceGroupName

#Provide the name of the Managed Disk
diskName=myDiskName

#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128


#Provide the URI of the VHD file that will be used to create Managed Disk. 
# VHD file can be deleted as soon as Managed Disk is created.
# e.g. https://contosostorageaccount1.blob.core.windows.net/vhds/contosovhd123.vhd 
vhdUri=https://contosostorageaccount1.blob.core.windows.net/vhds/contosoumd78620170425131836.vhd

#Provide the storage type for the Managed Disk. Premium_LRS or Standard_LRS.
storageType=Premium_LRS


#Provide the Azure location (e.g. westus) where Managed Disk will be located. 
#The location should be same as the location of the storage account where VHD file is stored.
#Get all the Azure location supported for your subscription using command below:
#az account list-locations
location=westus

#If you're creating an OS disk, uncomment the following lines and replace the values
#osType = 'yourOSType' #Acceptable values are either Windows or Linux
#hyperVGeneration = 'yourHyperVGen' #Acceptable values are either v1 or v2

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

#Create the Managed disk from the VHD file 
#If you're creating an OS disk, add the following: --os-type $osType -hyper-v-generation $hyperVGeneration 
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --location $location --size-gb $diskSize --source $vhdUri
# </FullScript>

腳本說明

此腳本會使用下列命令從 VHD 建立受控磁碟。 下表中的每個命令都會連結至命令特定的文件。

指令 備註
az disk create 使用相同訂閱中儲存帳戶中 VHD 的 URI 建立受控磁碟

後續步驟

將受控磁碟連結為OS磁碟,以建立虛擬機

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件

如需其他虛擬機和受控磁碟 CLI 腳本範例,請參閱 Azure Linux VM 檔案