Retrieve the size of the OS Disk in Resource Manager Azure Deployments using PowerShell
Recently I noticed that a script I was using wasn’t quite working correctly because it was failing to get the size of a Azure VM’s disk.
Usually I would use something similar to the following:
$VM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VirtualMachineName $size = $VM.StorageProfile.OsDisk.DiskSizeGB
However $VM.StorageProfile.OsDisk.DiskSizeGB
was null. I decided that I would write a function instead which would go and look at the underlying vhd blob and find the size that way. The below is my code and usage:
function Get-AzureRmVhdSize { param( [Uri] $uri = $(throw "Please enter a URI") ) $blobEndpoint = $uri.Scheme + "://" + $uri.Host + "/" $sa = Get-AzureRmStorageAccount | ? { $_.PrimaryEndpoints.Blob -eq $blobEndpoint } if ($sa -eq $null -or $sa.Length -ne 1) { Throw "Unable to locate storage account" } $containerAndBlob = $uri.AbsolutePath.Split("/", [StringSplitOptions]::RemoveEmptyEntries) $blob = Get-AzureStorageBlob -Blob $containerAndBlob[1] -Container $containerAndBlob[0] -Context $sa.Context $sizeInBytes = $blob.Length $sizeInGB = [int]($sizeInBytes/1073741824) return $sizeInGB } function Get-AzureRmOSDiskSize { param( [string]$VirtualMachineName = $(throw "Specify virtual machine name"), [string]$ResourceGroupName = $(throw "Specify resource group name") ) $VM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VirtualMachineName if ($VM -eq $null) { throw "Virtual Machine Not Found" } return Get-AzureRmVhdSize -uri $VM.StorageProfile.OsDisk.Vhd.Uri } Get-AzureRmOSDiskSize -VirtualMachineName testvm -ResourceGroupName testrg