In this article we’ll go over the following script that helps us remove resource reservations from all VMs within a VMware cluster.
Here is the script that we are going to use:
#Variable Declaration Param( [string]$server, [Parameter(Mandatory=$true)][string]$username, [Parameter(Mandatory=$true)][string]$password, [string]$cluster ) #Module Initialization Add-PSSnapin VMware.VimAutomation.Core #Body Connect-VIServer -Server $server -Username $username -Password $password $vms = Get-Cluster $cluster | Get-VM ForEach ($vm in $vms) { $reservation = Get-VMResourceConfiguration -VM $vm If ($reservation.CpuReservationMhz -gt 0) { Get-VM -Name $vm | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuReservationMhz 0 } }
The first part of the script is where all of our variables are declared as parameters. We have the server, username, password and cluster parameters where we specify the vCenter server we want to connect to, the username and password of an administrative user and the VMware cluster we want to connect to.
Next we import the VMware PowerCLI snap-in and connect to the vCenter server.
Next we get a list of all VMs in the cluster by using the Get-Cluster cmdlet, parsing its output to the Get-VM command and saving all the output in the $vms variable.
After that, we create a ForEach loop that goes through each VM in the array, retrieves the resource configuration for each VM using the Get-VMResourceConfiguration cmdlet and checks if the value of the CPUReservationMhz attribute is bigger than 0. If it is, then it configures the VM with 0 CPU Reservation.
It does that for all the VMs in the cluster. This type of script can also be modified and retrieve other types of configurations and so forth. Thank you for your time!