Working with disk limits in PowerCLI

This will be a post following on the previous one on how to control disk i/o in vSphere

That post showed how you set IOPS limits either through the UI or with PowerCLI.

Even though you set the limit on individual disks you need to work through the VM. And when you retrieve the disk limits for a VM you'll get back limits on disks identified by the device key and not the label.

To make this a bit easier to work with I've created some Powershell functions that wraps the *-VMResourceConfiguration cmdlets and one that gives you the label of the disks when you query the limits for a VM.

First of is the Get-VMDiskLimit which is essentially a wrapper for this command:

$Harddisk.Parent | Get-VMResourceConfiguration | Select-Object -ExpandProperty DiskResourceConfiguration |
Where-Object {$_.key -eq $Harddisk.ExtensionData.key} | 
Select-Object @{l="VM";e={$Harddisk.Parent.Name}},@{l="Label";e={$Harddisk.Name}},@{l="IOPSLimit";e={$_.DiskLimitIOPerSecond}}

It will output the limit of a disk in this way

Powershell function

We also have a function for setting limit on a disk which wraps this command

Get-VMResourceConfiguration -VM $Harddisk.parent |
Set-VMResourceConfiguration -Disk $Harddisk -DiskLimitIOPerSecond $IOPSLimit

Note that this is outputting the disk in the default format (with the key), but we can easily add the last line from the command shown above to have the same output

Powershell function output

Finally we have the function which will map the keys of the DiskResourceConfiguration output to the disk label names. This is done by mapping the key to the ExtensionData.Key property of the disks. We'll iterate over all disks for the VM and create a custom object which we add to an array which we finally output from the function.

$diskLimits = $v | Get-VMResourceConfiguration | Select-Object -ExpandProperty DiskResourceConfiguration
$disks = $v | Get-HardDisk
foreach($disk in $disks){
    $diskLimit = $diskLimits | Where-Object {$_.key -eq $disk.ExtensionData.Key}
    $o = [pscustomobject]@{
        VM = $v.Name
        Name = $disk.Name
        Key = $disk.ExtensionData.Key
        IOPSLimit = $diskLimit.DiskLimitIOPerSecond
    }
    $outputTbl += $o
}

The function works by specifying a VM name and outputs in this format. Note that it supports multiple vms

Powershell function usage

All functions can be found on GitHub.

This page was modified on April 1, 2019: Fixed categories and tags