Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

Q. Can I resize an Azure managed disk?
Q. How can I move to a smaller disk size in Azure?
Q. How can I create a VM in Azure with PowerShell to use managed disks?

Q. Can I resize an Azure managed disk?

Dept - Azure

A. Yes to a greater size. Stop the VM, resize the disk to a larger size then start the VM. You cannot shrink the size of an Azure managed disk.

Q. How can I move to a smaller disk size in Azure?

Dept - Azure

A. With standard unmanaged storage the direction was to create the biggest size possible, for example 1 TB, since only the data written was actually charged. This changes with premium storage and for managed disks (standard and premium) which bill based on the size of the disk and not the amount of data written. As customers look to move to managed disk they may find themselves in a position where they have 1 TB disks that have a minimal amount of data and want to move to a smaller disk to avoid paying for larger managed disks they do not need.

It is not possible to shrink a disk in Azure and so the process below is the best option to move to a smaller disk:

  1. Create a smaller unmanaged disk based on the target disk size
  2. Attach the disk to the VM
  3. Stop whatever services is writing to the current data disk
  4. Copy the data from the current data disk to the new data disk, for example:
    robocopy E:\ F:\ *.* /j /e /sec /Xd "System Volume Information" "$RECYCLE.BIN" /Xo
  5. Detach the larger data disk from the VM
  6. Change the drive letter to match that of the old data disk
  7. Restart the application

You could now switch to managed disks.

Note this only works for data disks. The only way to shrink the OS disk would be to download the VHD to on-premises, shrink the partition on the disk inside the guest OS, shrink the VHD, upload back to Azure then recreate the VM. Obviously this would incur a significant amount of downtime.

Q. How can I create a VM in Azure with PowerShell to use managed disks?

Dept - Azure

A. With managed disks there is no management or interaction with storage accounts, the managed disk is a first-class Azure resource with storage accounts automatically handled behind the scenes.

Using managed disks is very simple, for the most part you simply don't have to specify storage accounts. The PowerShell below creates a new VM using a standard managed disk for the OS disk and a premium managed disk for a data drive.

$vmname = "TestVM"
$IPAddr = "10.1.1.11"
Write-Output "Creating VM $vmname ($IPAddr)"
$user = "localadmin"
$password = 'Pa55word!'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)
$rgname = "Dal-Infra-RG"
$availSet = Get-AzureRmAvailabilitySet -Name "Dal-Infra-DC-AS" `
    -ResourceGroupName $rgname
$loc = "EastUS"
$networkname = "Prod-Dal-VNet"
$subnetname = "$networkname-Sub1"
$vnetRG = "Dal-VNets-RG"
$VNet = Get-AzureRmVirtualNetwork -Name $networkname -ResourceGroupName $vnetRG
$subnet = get-azurermvirtualnetworksubnetconfig -VirtualNetwork $vnet -Name $subnetname
$subnetID = $subnet.Id
# Create VM Object
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetId $availSet.Id

$nic = New-AzureRmNetworkInterface -Force -Name ('nic-' + $vmname) -ResourceGroupName $rgname `
-Location $loc -SubnetId $subnetID -PrivateIpAddress $IPAddr

# Add NIC to VM
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest

$vm = Set-AzureRmVMOSDisk -VM $vm -StorageAccountType StandardLRS -DiskSizeInGB 128 `
    -CreateOption FromImage -Caching ReadWrite -Name "$vmname-OS"

$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmname `
    -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

#Add data disk
$diskConfig = New-AzureRmDiskConfig -AccountType PremiumLRS -Location $loc -CreateOption Empty `
    -DiskSizeGB 128
$dataDisk1 = New-AzureRmDisk -DiskName "$vmname-data1" -Disk $diskConfig -ResourceGroupName $rgName

$vm = Add-AzureRmVMDataDisk -VM $vm -Name "$vmname-data1" -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1

#Create Virtual Machine
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm