How to convert a virtual machine to use managed disks using PowerShell
Overview
UKCloud for Microsoft Azure supports the use of managed disks on virtual machines (VMs). You can use managed disks as both OS disks and data disks.
For more information, see Introduction to Azure managed disks.
The following article shows you how to convert a virtual machine from unmanaged to managed disks on UKCloud for Microsoft Azure.
Warning
At the time of writing, the ConvertTo-AzVMManagedDisk cmdlet is not supported on Azure Stack Hub and will result in your VM becoming unmanageable. Follow the process below to convert your unmanaged disks safely.
Warning
Running the script below will result in downtime for your virtual machine as it needs to be removed then recreated.
Prerequisites
Before you begin, ensure your PowerShell environment is set up as detailed in Configure the Azure Stack Hub user's PowerShell environment.
Converting a virtual machine to use managed disks
Declare variables
Enter details below to provide values for the variables in the following script in this article:
Variable name | Variable description | Input |
---|---|---|
$ArmEndpoint | The Azure Resource Manager endpoint for Azure Stack Hub | |
$RGName | Name of the resource group that the VM exists in | |
$VMName | Name of the virtual machine to convert to use managed disks | |
$SAName | Name of the storage account that contains the unmanaged disks |
Convert the virtual machine
From your PowerShell window:
# Initialise environment and variables
# Declare endpoint
$ArmEndpoint = ""
## Add environment
Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
## Login
Connect-AzAccount -EnvironmentName "AzureStackUser"
# Get location of Azure Stack Hub
$Location = (Get-AzLocation).Location
# Input Variables
$RGName = ""
$VMName = ""
$SAName = ""
# Retrieve virtual machine details
$OldVM = Get-AzVM -ResourceGroupName $RGName -Name $VMName
# Remove the VM, keeping the disks
Write-Output -InputObject "Removing old virtual machine"
Remove-AzVM -Name $VMName -ResourceGroupName $RGName -Force
# Create OS managed disk
Write-Output -InputObject "Creating OS managed disk"
$SAId = (Get-AzStorageAccount -ResourceGroupName $RGName -Name $SAName).Id
$OSDiskConfig = New-AzDiskConfig -AccountType "Standard_LRS" -Location $Location -DiskSizeGB $OldVM.StorageProfile.OsDisk.DiskSizeGB `
-SourceUri $OldVM.StorageProfile.OsDisk.Vhd.Uri -StorageAccountId $SAId -CreateOption "Import"
$OSDisk = New-AzDisk -DiskName "$($OldVM.Name)_$($OldVM.StorageProfile.OsDisk.Name)" -Disk $OSDiskConfig -ResourceGroupName $RGName
# Create data managed disks
if ($OldVM.StorageProfile.DataDisks) {
$DataDiskArray = @()
foreach ($DataDisk in $OldVM.StorageProfile.DataDisks) {
Write-Output -InputObject "Creating data managed disk"
$DataDiskConfig = New-AzDiskConfig -AccountType "Standard_LRS" -Location $Location -DiskSizeGB $DataDisk.DiskSizeGB `
-SourceUri $DataDisk.Vhd.Uri -StorageAccountId $SAId -CreateOption "Import"
$DataDiskArray += New-AzDisk -DiskName "$($OldVM.Name)_$($DataDisk.Name)" -Disk $DataDiskConfig -ResourceGroupName $RGName
}
}
# Create new virtual machine config
$NewVMConfig = New-AzVMConfig -VMName $VMName -VMSize $OldVM.HardwareProfile.VmSize
# Add OS disk to the new virtual machine config
if ($OldVM.OSProfile.LinuxConfiguration) {
$NewVMConfig = Set-AzVMOSDisk -VM $NewVMConfig -ManagedDiskId $OSDisk.Id -CreateOption "Attach" -Linux
}
else {
$NewVMConfig = Set-AzVMOSDisk -VM $NewVMConfig -ManagedDiskId $OSDisk.Id -CreateOption "Attach" -Windows
}
# Add data disk(s) to the new virtual machine config
$Lun = 0
foreach ($Disk in $DataDiskArray) {
$NewVMConfig = Add-AzVMDataDisk -VM $NewVMConfig -ManagedDiskId $Disk.Id -CreateOption Attach -Lun $Lun -DiskSizeInGB $Disk.DiskSizeGB
$Lun++
}
# Add network interface card(s) to the new virtual machine config
foreach ($Nic in $OldVM.NetworkProfile.NetworkInterfaces) {
if ($Nic.Primary -eq $true -or $Nic.Primary -eq $null) {
$NewVMConfig = Add-AzVMNetworkInterface -VM $NewVMConfig -Id $Nic.Id -Primary
}
else {
$NewVMConfig = Add-AzVMNetworkInterface -VM $NewVMConfig -Id $Nic.Id
}
}
# Create the new virtual machine
Write-Output -InputObject "Creating new virtual machine"
New-AzVM -VM $NewVMConfig -ResourceGroupName $RGName -Location $Location
Get-AzVM -ResourceGroupName $RGName -Name $VMName
Write-Output -InputObject "The virtual machine has been created successfully"
Feedback
If you find a problem with this article, click Improve this Doc to make the change yourself or raise an issue in GitHub. If you have an idea for how we could improve any of our services, send an email to feedback@ukcloud.com.