VMware Cloud Community
TacoSauce
Enthusiast
Enthusiast

using powercli to control limit disk iops

Hi,

I've a lot of test vm's which are bullying our central storage. Now I've found the perfect script from Luc D which imports data from a csv file and then limits the disks.

Unfortunately I get red errors when I fire it up:

Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At H:\PowerCLI\VmIOPSLimit.ps1:13 char:22

+   $vm = Get-VM -Name $group.Name

+                      ~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-VM], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

You cannot call a method on a null-valued expression.

At H:\PowerCLI\VmIOPSLimit.ps1:23 char:3

+   $vm.ExtensionData.ReconfigVM_Task($spec)

+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

Can someone help me out? Here's the script from Luc D. which I found on the powercli community:

foreach($group in (Import-Csv C:\vmiopslimit.csv -UseCulture | Group-Object -Property vmName)){   $vm = Get-VM -Name $group.Name
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $vm.ExtensionData.Config.Hardware.Device |  where {$_ -is [VMware.Vim.VirtualDisk]} | %{     $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $dev.Operation = "edit"
    $dev.Device = $_
    $label = $_.DeviceInfo.Label
    $dev.Device.StorageIOAllocation.Limit = $group.Group | where {$_.Harddisk -eq $label} | Select -ExpandProperty IOPSLimit
    $spec.DeviceChange += $dev
  }  
$vm.ExtensionData.ReconfigVM_Task($spec) }

The CSV file should look like this

"vmName","harddisk","IOPSLimit"
"VM1","Hard disk 1","50"
"VM1","Hard disk 2","75"

"VM2","Hard disk 1","75"

"VM3","Hard disk 1","35"
"VM3","Hard disk 2","55"

"VM3","Hard disk 3","65"

0 Kudos
4 Replies
LucD
Leadership
Leadership

The script should work, I include it again because there seem to be some <cr><lf> issues.

foreach($group in (Import-Csv C:\vmiopslimit.csv -UseCulture | Group-Object -Property vmName)){
 
$vm = Get-VM -Name $group.Name
 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
 
$vm.ExtensionData.Config.Hardware.Device |  where {$_ -is [VMware.Vim.VirtualDisk]} | %{
   
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
   
$dev.Operation = "edit"
   
$dev.Device = $_
   
$label = $_.DeviceInfo.Label
   
$dev.Device.StorageIOAllocation.Limit = $group.Group | where {$_.Harddisk -eq $label} | Select -ExpandProperty IOPSLimit
   
$spec.DeviceChange += $dev
  }
 
$vm.ExtensionData.ReconfigVM_Task($spec)
}

Are you sure there are no blank lines in the CSV file ? Perhaps at the end ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
TacoSauce
Enthusiast
Enthusiast

Hi Luc,

Thnx for your quick reply!

Here's a screenshot of the csv file, (notepad++  "Show All Characters" selected)

ScreenShot038.jpg

And here is the output from the PowerShell Console:

ScreenShot039.jpg

0 Kudos
LucD
Leadership
Leadership

The problem is definitely with the content of the $group.Name property.

At the moment the Get-VM cmdlet is executed, there seems to be no value in there.

You could display the content of $group.Name before calling the Get-VM cmdlet

An interactive debugger would be even better, then you could place a breakpoint on the line with the Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
TacoSauce
Enthusiast
Enthusiast

Okay, I will give it a try. I will give a response next week..

0 Kudos