How to resize disk of virtual machine using Azure.Net SDK/ Fluent API

217 views Asked by At

I want to resize my os disk space but getting "Disk resizing is allowed only when creating a VM or when the VM is deallocated."

Microsoft.Azure.Management.ResourceManager.Fluent.Core.RestClient restClient = GetRestClient(requestObject);
            var computeClient = new ComputeManagementClient(restClient) { SubscriptionId = requestObject.SubscriptionId };
            var ObjVirtualMachines = computeClient.VirtualMachines.GetAsync(requestObject.ResourceGroupName, requestObject.VirtualMachineName, null, new System.Threading.CancellationToken()).Result;
            var disktodetach = ObjVirtualMachines.StorageProfile.OsDisk;
            ObjVirtualMachines.StorageProfile.OsDisk.DiskSizeGB = requestObject.DiskSize;
            var newUpdateVM = computeClient.VirtualMachines.CreateOrUpdateAsync(requestObject.ResourceGroupName, requestObject.VirtualMachineName, ObjVirtualMachines).Result;
            resizeDisk = newUpdateVM.ProvisioningState;
1

There are 1 answers

0
SaiSakethGuduru On

Thank you 1_bug. Posting your suggestions as answer to help other community members.

you cannot Increase the disk size for a running VM. Rather you can increase the disk size of a VM by shutting down the VM and following the below code.

[Newtonsoft.Json.JsonProperty(PropertyName="properties.diskSizeGB")]
public int? DiskSizeGB { get; set; }

enter image description here

Before using the above code check weather your code includes creationData.creationoption field is empty.

for further information check Disk.DiskSizeGB Property