Resize Azure Windows OS disk script.
What happens when you have a Windows VM in Azure and you need to resize the disk size? You can’t do this in the portal, you need to use Powershell.
The full script
1Function Resize-VM-OS-Disk(
2 [String]$SubscriptionName,
3 [String]$ResourceGroupName,
4 [String]$VmName,
5 [Int32]$NewDiskSize
6)
7{
8 Connect-AzAccount
9
10 Select-AzSubscription –SubscriptionName $SubscriptionName
11
12 $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $vmName
13
14 Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $vmName
15
16
17 $disk= Get-AzureRmDisk -ResourceGroupName $ResourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name
18 $disk.DiskSizeGB = $NewDiskSize
19 Update-AzDisk -ResourceGroupName $ResourceGroupName -Disk $disk -DiskName $disk.Name
20
21 Start-AzVM -ResourceGroupName $ResourceGroupName -Name $vmName
22}
Example to call the above script:
1Resize-VM-OS-Disk -VmName "VM_NAME" -SubscriptionName "SUBSCRIPTION_NAME" -ResourceGroupName "RESOURCE_GROUP_NAME" -NewDiskSize 64