PowerCLI: Enable/Disable the VM Hot Add features


image

Since the release of vSphere, you are able to Hot Add memory and vCPU. Quote from the VMware website:

Virtual Machine Hot Add Support— The new virtual hardware introduced in ESX/ESXi 4.0 supports hot plug for virtual devices and supports addition of virtual CPUs and memory to a virtual machine without powering off the virtual machine. See the Guest Operating System Installation Guide for the list of operating systems for which this functionality is supported.

 

So I wanted to see, if I was able to enable/disable this settings via PowerCLI and came up with a couple of functions.

The first function enables the Memory Hot Add feature:

Function Enable-MemHotAdd($vm){
    $vmview = Get-vm $vm | Get-View 
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}

You can run the function via the following command:

Enable-MemHotAdd vc01

When you verify the settings in the vSphere Client, You’ll see that the Memory Hot Add feature is enabled.

image

There is only one problem, the setting doesn’t work. You have to shutdown and start the VM, before you are able to hot add memory to the VM. When the VM is started again, I was able to Hot Add extra memory 🙂

image

To add extra memory via PowerCLI, You have to run the following command:

Get-VM -Name "vc01" | Set-VM -MemoryMB "3072"

 

You can use the next function to disable the setting:

Function Disable-MemHotAdd($vm){
    $vmview = Get-VM $vm | Get-View 
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}

 

I have also created two functions which you can use to enable or disable the hot add feature for vCPU’s:

Enable:

Function Enable-vCpuHotAdd($vm){
    $vmview = Get-vm $vm | Get-View 
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}

Disable:

Function Disable-vCpuHotAdd($vm){
    $vmview = Get-vm $vm | Get-View 
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra

    $vmview.ReconfigVM($vmConfigSpec)
}
Advertisements

11 thoughts on “PowerCLI: Enable/Disable the VM Hot Add features

  1. Pingback: PowerCLI: Check CPU/Memory Hot Add « ICT-Freak.nl

    • if you add Get-Cluster | Get-VM or Get-VMHost | Get-VM you can run it against all VM’s in a Cluster or on a specific host.

  2. the script does work, thanks a lot!

    However, looks like even the script is enabling memory (cpu) hot add on live VM you still cannot adjust (add) memory before VM will be powered off/on. Is it correct? Is there a way to fix it through the script?

    Thanks a lot

  3. Function Enable-HotAdd{
    [CmdletBinding()]
    [OutputType([string])]
    Param
    (
    # A VM name
    [Parameter(Mandatory=$true,
    ValueFromPipelineByPropertyName=$true,
    Position=0)]
    [string]$VM,

    # Component to enable (Mem or CPU)
    [Parameter(Mandatory=$true,
    ValueFromPipelineByPropertyName=$false,
    Position=1)]
    [ValidateSet(“CPU”,”Mem”)]
    [array]$Device
    )

    Begin
    {
    $targVM = Get-VM $VM
    $vmview = $targVM | Get-View
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    foreach($Dev in $Device){
    switch ($Dev)
    {
    ‘Mem’ {
    $extraMem = New-Object VMware.Vim.optionvalue
    $extraMem.Key=”mem.hotadd”
    $extraMem.Value=”true”
    $vmConfigSpec.extraconfig += $extraMem
    }
    ‘CPU’ {
    $extraCpu = New-Object VMware.Vim.optionvalue
    $extraCpu.Key=”vcpu.hotadd”
    $extraCpu.Value=”true”
    $vmConfigSpec.extraconfig += $extraCpu
    }
    }
    }
    }
    Process
    {
    $vmview.ReconfigVM($vmConfigSpec)
    }
    End
    {
    $hot=$vmConfigSpec.ExtraConfig|where{$_.Key -like ‘*hotadd’}
    write-host $targVM.name $hot[0].Key $hot[0].Value $hot[1].Key $hot[1].Value
    }
    }

  4. Any chance you would be wiling to create a script to disable the hotplug vNic ability under configuration parameters and post that? Right now I power down a vm, then edit, advanced configurations and add the row ‘devices.hotplug’ with parameter of ‘false’ trying to do this for the entire inventory at once using powercli but not getting to far.

    Would be very much appreciated!

  5. Pingback: Informations about CPU- and RAM-HotPlug at VMs on VMware vSphere 5 › Virtual Professional

  6. the doc says “You have to shutdown and start the VM, before you are able to hot add memory to the VM. When the VM is started again, I was able to Hot Add extra memory”

    That confused me a bit.

    But the actual flow of event should be :-

    1. Power off VM
    2. Run the functions
    3. Power On the VM

  7. You rock! This worked perfectly when I wasn’t able to edit a version 10 VM since the host it was on was less than that version.

  8. Pingback: Function Enable/Disable CPU/Mem Hot Add – VM AT WORDPRESS

  9. This doesn’t work for vcenter 6.7. Works fine for 6.5 though. Anyone have any luck setting hotadd with vcenter 6.7? I’ve tried older version and latest Powercli 11.3 but neither work with vcenter 6.7 when using this script to set hotadd.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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