Use existing VHD for AzureRM VM

Use an existing VHD when creating a new VM with AzureRM

John Savill

January 15, 2016

1 Min Read
Use existing VHD for AzureRM VM

Q. How do I use an existing VHD when creating a VM using AzureRM?

A. Normally when creating a new VM you have code to set a credential, set an OS type and select a base image. For example:

$osDiskName = $vmname+'_osDisk'$osDiskCaching = 'ReadWrite'$osDiskVhdUri = "https://$stoname.blob.core.windows.net/vhds/"+$vmname+"_os.vhd"# Setup OS & Image$user = "localadmin"$password = 'Pa55word5'$securePassword = ConvertTo-SecureString $password -AsPlainText -Force$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword) $vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname -Credential $cred$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName $AzureImage.PublisherName -Offer $AzureImage.Offer `    -Skus $AzureImage.Skus -Version $AzureImage.Version$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption fromImage -Caching $osDiskCaching

To use an existing disk replace all of the above with:

$osDiskName = $vmname+'_osDisk'$osDiskCaching = 'ReadWrite'$osDiskVhdUri = "https://$stoname.blob.core.windows.net/vhds/"+$vmname+"_os.vhd"$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Windows -Caching $osDiskCaching

Note you still set variables for the disk name, caching and location but the attach create option is used instead of fromImage.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like