0

I have been trying to create a script that can get a list of all managed disks in Azure, which includie vmname, diskname, size, OSType and resource group name and more importantly free space and available disk space remaining.

I can get the basic information using Powershell, however I can't seem to get current disk usage and free space remaining, I was hoping some guidance or point in the right direction.

$vms = Get-AzureRmVM 
foreach ($vm in $vms) {
Get-AzureRmDisk | select -Property $vm.Name,Name,DiskSizeGB,OSType,ResourceGroupName
}

Thanks in advance

Norrin Rad
  • 881
  • 2
  • 18
  • 42

2 Answers2

2

Based on my knowledge, it is not possible. You could check managed disk rest APi. Azure does not provide such parameters.

So, you could not use Azure Power Shell to get managed disk usage information, you only could get total space.

One solution, you need login your VM and use command to get your disk usage.

Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • @Shenbao Shui - MSFT Thanks for the info, do you mean logging onto each vm and running a powershell command that gives you disk info, something like a wmi query win32_logicaldisk .. does that work in Azure? – Norrin Rad Feb 14 '18 at 07:27
  • @NorrinRad Yes, I think it should work. But in your VM, you should mount this managed disk. – Shui shengbao Feb 14 '18 at 07:31
  • @NorrinRad Like this [answer](https://stackoverflow.com/questions/12159341/how-to-get-disk-capacity-and-free-space-of-remote-computer). – Shui shengbao Feb 14 '18 at 07:34
  • 1
    that’s great, what if I have 30 VMs per subscription, 4 subscription over 2 tenants, basically we want to check disk usage so we can look at reducing disk size as part of a money saving exercise. Hope that makes sense – Norrin Rad Feb 14 '18 at 07:35
  • @NorrinRad Managed disk is different from unmanaged disk. The managed disk cost depends on the total size. In other word, if you create a 32 GB size managed disk, but you only use 1 GB. You also need pay for 32 GB. – Shui shengbao Feb 14 '18 at 07:37
  • Yep that’s what I thought so we are thinking of reducing disk size to what we need, before we paid for what we used now we pay for what we provision. If I did a Get-AzureRmVm and foreach get-win32_logicaldisk. Would that give me the disk sizes and usage – Norrin Rad Feb 14 '18 at 07:40
  • @NorrinRad You could check this [link](https://azure.microsoft.com/en-us/pricing/details/managed-disks/). Managed disk will be treated as the next available managed size. If you create a 20 GB managed disk, you also need pay for 32 GB(S4) money. If you create a 33GB managed disk, you need pay for 64GB(S6) money. – Shui shengbao Feb 14 '18 at 07:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165109/discussion-between-shengbao-shui-msft-and-norrin-rad). – Shui shengbao Feb 14 '18 at 07:41
  • I suggest you could select disk size like 32GB or 64GB. This is the most economical and you could use more disk space. – Shui shengbao Feb 14 '18 at 07:45
  • One more important that Azure only support expand a disk size. Reduce a disk size is not supported. – Shui shengbao Feb 14 '18 at 08:07
  • Thanks, I didn’t know that. So are we saying you get charged for 32gb, 64gb etc if those are the supported sizes, so even if you only have 10gb and 40gb on each disk and you can’t reduce to 15gb and 50gb – Norrin Rad Feb 14 '18 at 08:13
1

You also could use a Hybrid-Worker and run this Command:

Getting all local Disks (exclude the Temp-Drive D)

$Disks = get-WmiObject win32_logicaldisk `
| Where-Object -Property VolumeName -ne "Temporary Storage"

Calculate the free Space in Percent and create a PS Custom Object

$Report = @()
foreach ($Disk in $Disks) {
    $myObject = [PSCustomObject]@{
        Hostname     = hostname
        Disk         = $Disk.DeviceID
        FreeSpace    = [math]::Round((($Disk.FreeSpace * 100) / $Disk.Size),0)
    }
    $Report += $myObject
}

and store the Result in a StorageAccount Table

Gill-Bates
  • 559
  • 8
  • 22