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.
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 🙂
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) }
How could this script be modified to run against an entire cluster or specific hosts?
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.
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
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
}
}
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!
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
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.
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.
# tested in vcenter 6.7.0.14368073
Function Enable-HotAdd($vmName){
$vm = Get-VM $vmName
if ( $VM.PowerState -eq “PoweredOff” ) {
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.memoryHotAddEnabled = $true
$spec.cpuHotAddEnabled = $true
$vm.ExtensionData.ReconfigVM_Task($spec)
}
else { write-host “Cannot enable hot-add with VM powered-on.” }
}
# tested in vcenter 6.7.0.14368073
Function Enable-HotAdd($vmName){
$vm = Get-VM $vmName
if ( $VM.PowerState -eq “PoweredOff” ) {
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.memoryHotAddEnabled = $true
$spec.cpuHotAddEnabled = $true
$vm.ExtensionData.ReconfigVM_Task($spec)
}
else { write-host “Cannot enable hot-add with VM powered-on.” }
}
Hi there,
Can you please suggest what I need for 6.7 , I need to use CSV file since there are well over 100VMs to disable hot add CPU/RAM. Your help will be appreciable. Thanks in advance