VMware Cloud Community
klich
Enthusiast
Enthusiast
Jump to solution

configuring shares, reservations, and limits

Anybody figure out how to configure shares, reservations, and limits yet? and is willing to share

I'm looking to do what Set-ResourcePool does, at a VM level.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The form destroyed the script layout.

Here it is in a more readable format (I hope)

Get-VM |% {Get-View $_.ID} |`

% {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;

$spec.CPUAllocation = New-Object VMware.Vim.ResourceAllocationInfo;

$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;

$spec.CpuAllocation.Shares.Level = "low";

$spec.CpuAllocation.Limit = -1;

Get-View($_.ReconfigVM_Task($spec))}


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

View solution in original post

0 Kudos
19 Replies
Niket
Enthusiast
Enthusiast
Jump to solution

Hi Klich,

Configuring shares, reservations, and limits can be done at resourcepool using Set-ResourcePool cmdlet.You can use Set-VM cmdlet to set memory and numcpu at virtual machine level.

To set shares, reservation and limits at virtual machine you should use the function ReconfigVM with the specification.

Please refer the below code for the same.

$server = Get-VIServer -Server

$vms = Find-EntityViews -ViewType "VirtualMachine"

$Spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.CpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo

$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo

$spec.CpuAllocation.Shares.Level = "normal"

$spec.CpuAllocation.Shares.Shares = 3000

foreach($vm in $vms) {

$vm.ReconfigVM($spec)

}

Thanks

Niket

LucD
Leadership
Leadership
Jump to solution

The script didn't work for me.

The shares.shares property was not the one to use to configure the limit property.

I also converted the script for the get-view method. (find-entityview is depreciated in the beta)

Get-VM |% {Get-View $_.ID} |`

% {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;

$spec.CPUAllocation = New-Object VMware.Vim.ResourceAllocationInfo;

+ $spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;+

+ $spec.CpuAllocation.Shares.Level = "low";+

$spec.CpuAllocation.Limit = -1;

Get-View($_.ReconfigVM_Task($spec))}

Fyi, a limit of -1 corresponds with Unlimited.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The form destroyed the script layout.

Here it is in a more readable format (I hope)

Get-VM |% {Get-View $_.ID} |`

% {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;

$spec.CPUAllocation = New-Object VMware.Vim.ResourceAllocationInfo;

$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;

$spec.CpuAllocation.Shares.Level = "low";

$spec.CpuAllocation.Limit = -1;

Get-View($_.ReconfigVM_Task($spec))}


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

0 Kudos
klich
Enthusiast
Enthusiast
Jump to solution

Exactly what I needed. Glad to see it done so nicely.

0 Kudos
klich
Enthusiast
Enthusiast
Jump to solution

Here is the code to reconfigure the memory reservations, since I needed to do that as well. Hopefully it will help somebody else.

Get-VM |% {Get-View $_.ID} |`

% {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;

$spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo;

$spec.memoryAllocation.Shares = New-Object VMware.Vim.SharesInfo;

$spec.memoryAllocation.Shares.Level = "normal";

$spec.memoryAllocation.Limit = -1;

Get-View($_.ReconfigVM_Task($spec))}

0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello All,

I'm trying to query the values of memory limit and reservation and cpu limit and reservation on a per VM basis ? Does somebody knows the syntax for that ? I'm trying to understand the way VI-Toolit is working.. But on this point no luck so far..

0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Use "Get-VM" to map a variable to the vm you want (e.g.: $VM = Get-VM -Name "testvm")

When you look at the properties (just type $VM), you'll see the most usefull properties available. And there is a property called ID.

You can use this ID property with the Get-View cmdlet to get all properties available in the SDK. (e.g.: $VMadv = Get-View -MoRef $VM.ID)

Now take a look at the properties available to you! (by typing $VMadv)

Hope this helps!

EDIT: By the way, the properties are nested. The ones you are looking for can be found in $VMadv.Summary.Config

0 Kudos
dmaster
VMware Employee
VMware Employee
Jump to solution

Hello Hugo,

Thanx for helping out, this is exactly what i'm looking for.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If I understand your question correctly you want get these values for all your virtual guests.

And that is one of the things Powershell can do best; loop through a collection of values.

This is a sample script that does approximately what you want.


get-vm | %{Get-View $_.ID} | %{
  Write-Host "VM name: " $_.Name
  write-host " CPU shares: " $_.Config.CpuAllocation.Shares.Level
  if($_.Config.CpuAllocation.Limit -eq -1){
    write-host " CPU limit: unlimited"
  }
  else{
    write-host " CPU limit: " $_.Config.CpuAllocation.Limit
  }

  Write-Host " Memory shares: " $_.Config.MemoryAllocation.Shares.Level
  if($_.Config.MemoryAllocation.Limit -eq -1){
    write-host " Memory limit: unlimited"
  }
  else{
    write-host " Memory limit: " $_.Config.CpuAllocation.Limit
  }
}

Note1: the Get-VM cmdlet will query the VC for all guests that are defined under the VC

Note2: the Get-VM cmdlet returns a collection of objects where each object represents a guest (the VI Toolkit way)

Note3: you pass this collection to the next cmdlet through the pipe (|)

Note4: foreach object (% is in fact an alias for the foreach-object cmdlet) that comes through the pipe you execute the next script block (everything between the {...})

Note5: every object that comes through the pipe is represented by the $_ variable

Note6: with the Get-View cmdlet you get the SDK object that corresponds with the VI Toolkit object

Note7: you pipe all the SDK objects to the next script block

Note8: the VirtualMachine object is documented in the SDK API Reference

Note9: in this script block the script prints the values you are interested in

Note10: as explained in the ResourceAllocationInfo object when a limit is set to -1 it is in fact unlimited


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

0 Kudos
aaronlundber
Contributor
Contributor
Jump to solution

I tried the script to change the CPU and Memory shares on some of my VMS, but I keep getting errors saying: 

You cannot call a method on a null-valued expression.
At line:14 char:1
+ Get-View($_.ReconfigVM_Task($spec))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


I have "$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;" at line 14. Any solution for this 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you post the complete script you are trying to run?
I'm not sure what you have in $_


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

0 Kudos
aaronlundber
Contributor
Contributor
Jump to solution

Hello LucD: 

Here is the script I am running. I want to configure specs of VMS that are in a folder. I also need the same to change Hard Disk shares from normal to  "low". I tried myself but couldn't figure it out. 

Get-VM -Location (Get-Folder Windows_Server) 

% {$spec = new-object VMware.Vim.VirtualMachineConfigSpec;

$spec.CPUAllocation = New-Object VMware.Vim.ResourceAllocationInfo;

$spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;

$spec.CpuAllocation.Shares.Level = "low";

$spec.CpuAllocation.Limit = -1;

Get-View($_.ReconfigVM_Task($spec))}

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You don't seem to pipe the Get-VM objects to the codeblock

Get-VM -Location (Get-Folder Windows_Server) | %{
    $spec = new-object VMware.Vim.VirtualMachineConfigSpec;
    $spec.CPUAllocation = New-Object VMware.Vim.ResourceAllocationInfo;
    $spec.CpuAllocation.Shares = New-Object VMware.Vim.SharesInfo;
    $spec.CpuAllocation.Shares.Level = "low";
    $spec.CpuAllocation.Limit = -1;
    Get-View($_.ReconfigVM_Task($spec))
}


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

0 Kudos
aaronlundber
Contributor
Contributor
Jump to solution

Thank You, 
Do you know how I can change the Hard Disk? I also want to configure the hard disk shares?

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You use the StorageIOAllocation property on the VirtualDisk object(s)

 


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

0 Kudos
aaronlundber
Contributor
Contributor
Jump to solution

I tried the below script but it didn't work : 

Get-VM -Location (Get-Folder MS_Windows_Server) | %{

    $spec = new-object VMware.Vim.StorageResourceManager.IOAllocationInfo;

    $spec.VirtualDisk = New-Object Vmware.Vim.vm.device.VirtualDisk;

    $spec.VirtualDisk.Shares = New-Object VMware.Vim.SharesInfo
    $spec.VirtualDisk.Shares.Level = "low";
    $spec.VirtualDisk.Limit = -1;
    Get-View($_.ReconfigVM_Task($spec))
}

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Changes to devices, like VirtualDisk, go under the DeviceChange property in the VirtualMachineConfigSpec object.


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

0 Kudos
aaronlundber
Contributor
Contributor
Jump to solution

I tried but couldn't get make it work. Can you please post it here how to change the shares level for Virtual Hard Disk. It will be much appreciated. Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you open a new thread for that question?


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

0 Kudos