Skip to content

Latest commit

 

History

History
148 lines (111 loc) · 7.99 KB

azs-how-convert-to-managed-disks-ps.md

File metadata and controls

148 lines (111 loc) · 7.99 KB
title description services author reviewer lastreviewed toc_rootlink toc_sub1 toc_sub2 toc_sub3 toc_sub4 toc_title toc_fullpath toc_mdlink
How to convert a virtual machine to use managed disks using PowerShell
Provides help for converting a virtual machine from unmanaged to managed disks on UKCloud for Microsoft Azure
azure-stack
blawson
wturner
06/12/2021
Users
How To
Convert a VM to use managed disks - PowerShell
Users/How To/azs-how-convert-to-managed-disks-ps.md
azs-how-convert-to-managed-disks-ps.md


UKCloud Limited (“UKC”) and Virtual Infrastructure Group Limited (“VIG”) (together “the Companies”) – in Compulsory Liquidation

On 25 October 2022, the Companies were placed into Liquidation with the Official Receiver appointed as Liquidator and J Robinson and A M Hudson simultaneously appointed as Special Managers to manage the liquidation process on behalf of the Official Receiver.

Further information regarding the Liquidations can be found here: https://www.gov.uk/government/news/virtual-infrastructure-group-limited-and-ukcloud-limited-information-for-creditors-and-interested-parties

Contact details:
For any general queries relating to the Liquidations please email ukcloud@uk.ey.com
For customer related queries please email ukcloudcustomers@uk.ey.com
For supplier related queries please email ukcloudsuppliers@uk.ey.com

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 = "https://management.frn00006.azure.ukcloud.com"

## 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 = "MyResourceGroup"
$VMName = "MyVM"
$SAName = "MyStorageAccount"

# 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.