Change bulk VM Specs

      1 Comment on Change bulk VM Specs

One common need is to modify a group of VMs according to certain parameters. A couple VMs can be done on the GUI but a few hundred or thousand can be a tedious process. I’m going to give you a quick example but the possibilities are unlimited.

Suppose you want to change the latency sensitivity in a cluster or datacenter, you can do something like this

$temp=get-vm 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.latencySensitivity = New-Object VMware.Vim.LatencySensitivity
$spec.LatencySensitivity.Level = [VMware.Vim.LatencySensitivitySensitivityLevel]::high

foreach ($VM in $temp) {
(Get-VM $VM).ExtensionData.ReconfigVM_Task($spec)
Write-host $VM.name " has been updated!"
}

This is the simplest form as we are only changing a single parameter, but we can change any attributes and apply them in one go.
In a few steps here is what we did:

  1. Got a list of VMs that can then be processed
  2. Created a spec object which will hold all the specs
  3. Changed the required parameters
  4. Looped through all VMs applying the spec

Great stuff, but how about if we want to be more granular, like update only VM’s that start with “DC” all we need to do is change the first line $temp=get-vm -Name "DC*"

Now we only have DC vms in our array but we may want to apply different spec depending on VM location?
Adding the below code within the loop can achieve that (this off course depends on your naming convention)
if (($VM.name -like '*-Cork') -or ($VM.name -like '*-London')){

Final code could be something like this

get-vm -Name "DC*"
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.latencySensitivity = New-Object VMware.Vim.LatencySensitivity
$spec.LatencySensitivity.Level = [VMware.Vim.LatencySensitivitySensitivityLevel]::high

foreach ($VM in $temp) {
if (($VM.name -like '*-Cork') -or ($VM.name -like '*-London')){
(Get-VM $VM).ExtensionData.ReconfigVM_Task($spec)
Write-host $VM.name " has been updated!"
}
}

You can find a find more examples here and API docs here

One thought on “Change bulk VM Specs

Leave a Reply

Your email address will not be published. Required fields are marked *