Tag Archives: hotadd vcpu

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   
}