Batch convert your #Azure VMs to #manageddisks


AS you may know managed disks have been released a couple of weeks ago, your current virtual machines can be converted to managed disks with just a reboot and a limited downtime.
The following is a quick script to batch convert all the virtual machines in a subscription.
Things to know:

  1. do it at your own risk and be prepared for some glitches and retries
  2. once you move to managed disks you start to be billed by disk size and not actual used space (inside the disk)
  3. the migration process runs in background so it takes time before the disks are can be removed from the old storage accounts
  4. currently it is not clear (to me at least) if the .status fles can be removed or not. More once I know more.

Official documentation for migrating to managed disks

login-azurermaccount

Get-AzureRmSubscription | Out-GridView -o Single | Select-AzureRmSubscription

#batch update all the AS in the subscription

$avsets = Get-AzureRmResourceGroup | Get-AzureRmAvailabilitySet

foreach($avset in $avsets) {
    Update-AzureRmAvailabilitySet -AvailabilitySet $avSet -Managed
    $vms = Get-AzureRMVM -ResourceGroupName $avSet.ResourceGroupName -Status
    foreach($vmInfo in $avSet.VirtualMachinesReferences)
    {
       $vm =  $vms | Where-Object {$_.Id -eq $vmInfo.id}

       write-host "Processing $($vm.Name)"
       if ($vm.StorageProfile.OsDisk.Vhd) {
               write-host "    migrating"
               if($vm.PowerState -ieq 'running') {
                Stop-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name  $vm.Name -Force
                $restart=$true
               } else {$restart=$false}

               ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name
               if($restart) {Start-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name  $vm.Name}
       }
    }
}

#converty all the VMs not in a availability set

$vms= Get-AzureRmVM -Status
foreach($vm in $vms) {
      write-host "Processing $($vm.Name)"
       if ($vm.StorageProfile.OsDisk.Vhd) {
               write-host "    migrating"
               if($vm.PowerState -ieq 'running') {
                Stop-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name  $vm.Name -Force
                $restart=$true
               } else {$restart=$false}

               ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name
               if($restart) {Start-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name  $vm.Name}
       }

}

Advertisements

,

  1. System Center Mart 2017 Bülten – IT-News von PC-Meister
  2. System Center Mart 2017 Bülten – Sertaç Topal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: