Enable Hot Add vCPU and Memory on VMs using PowerShell

I’ve got a new version of this script at this link.

This is a simple script which will allow you to enable the Hot Add vCPU and Memory feature on all or a sub set of your VMs.

IMPORTANT NOTE – the script will recursively power off each VM defined in CSV, apply the config then power each VM back on. So make sure you run this script in an outage window.

When creating the CSV, make sure to include the header row – Name.

#Function to generate wait time progress bar
function Start-Sleep($seconds) {
    $doneDT = (Get-Date).AddSeconds($seconds)
    while($doneDT -gt (Get-Date)) {
    $secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds
    $percent = ($seconds - $secondsLeft) / $seconds * 100
    Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining 
    $secondsLeft -PercentComplete $percent
    [System.Threading.Thread]::Sleep(500)
    }
Write-Progress -Activity "Sleeping" -Status "Sleeping..." -SecondsRemaining 0 - 
Completed
}

#Variables
$HotAddVMs = Import-CSV c:\temp\hot_add_disabled_vms.csv
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.CpuHotAddEnabled = "True"
$vmConfigSpec.MemoryHotAddEnabled = "True"

Foreach ($VM in ($HotAddVMs)) {
    
    #Shutdown Guest OS / Power off VM
    Get-VM $VM.Name | Shutdown-VMGuest -Confirm:$false

    #Wait for VM to power off before executing config cmd  
    section – in which case the config cmd will fail 
    while((Get-VM $VM.Name).PowerState -ne 'PoweredOff') {

    Start-Sleep -Seconds 10
    }
    
    #Execute config cmd
    $VMConfig = Get-View -VIObject $VM.Name
    $VMConfig.ReconfigVM($vmConfigSpec)

    #Power on VM
    Get-VM $VM.Name | Start-VM   
}


One thought on “Enable Hot Add vCPU and Memory on VMs using PowerShell

  1. TM

    I am new to the vmware world, and your post is almost exactly what I need to do….but opposite, I need to disable the cpu hot add feature on my vms, I also need to adjust the CPU, Sockets, and Core amounts on a number of my VMs….I am wondering if there was a way to do that through a script

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.