Imagine one of your VMs struggling for compute resource and you want to add more resource without powering off the VM? Well VMware hot add and hot plug allows you to do just that. Hot-add vMem and hot-plug vCPU should allow you to easily add vMem and vCPU as necessary while your virtual machine is running (no downtime required if the feature is enabled). This post is not about explaining what these are, you can read more from VMware here KB1012225.
If you add more RAM to a running VM, then you are ‘hot-adding’ it. If you add another virtual CPU (vCPU) to a running VM then you are ‘hot-plugging’. It would be incorrect to say that you used hot-plug RAM or hot-add CPU. Below is a script on how to make these changes.
VM CPU and Memory Hot Add Check Status
#Create a .ps1 C:\scripts\HotAddHotPlugStatus.ps1
#Import-Module VMware.VimAutomation.Core
Import-Module VMware.VimAutomation.PCloud
$VCServer = Read-Host 'Enter VC Server name'
$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)
$LogFile = "VMHotAddHotPlugStatus_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Result = @()
If($? -Eq $True)
{
Write-Host "Connected" -Foregroundcolor "Green"
Write-Host "Collecting Hot Plug Status of the VMS ..." -Foregroundcolor "Yellow" -NoNewLine
$Results = @()
$Result = (Get-VM | select ExtensionData).ExtensionData.config | Select Name, MemoryHotAddEnabled, CpuHotAddEnabled, CpuHotRemoveEnabled
$Result | Export-Csv -NoTypeInformation $LogFile
Write-Host "Completed" -Foregroundcolor "Green"
}
Else
{
Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}
VM CPU and Memory Hot Add - This Add HotPlug
#Create this file in Scripts folder as per below C:\scripts\csv\csvfile.csv and save as .ps1
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)
}
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)
}
$vmlist = Import-CSV C:\scripts\csv\csvfile.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
Stop-VM -VM $vmname -RunAsync
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
$cpu = $item.cpu
$mem = [int]$item.mem * 1024
Enable-MemHotAdd $vmname
Enable-vCpuHotAdd $vmname
Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -RunAsync -Confirm:$false
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
Start-VM -VM $vmname -RunAsync
}
VM CPU and Memory Hot Add Check Status
#Create a .ps1 C:\scripts\HotAddHotPlugStatus.ps1
#Import-Module VMware.VimAutomation.Core
Import-Module VMware.VimAutomation.PCloud
$VCServer = Read-Host 'Enter VC Server name'
$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)
$LogFile = "VMHotAddHotPlugStatus_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Result = @()
If($? -Eq $True)
{
Write-Host "Connected" -Foregroundcolor "Green"
Write-Host "Collecting Hot Plug Status of the VMS ..." -Foregroundcolor "Yellow" -NoNewLine
$Results = @()
$Result = (Get-VM | select ExtensionData).ExtensionData.config | Select Name, MemoryHotAddEnabled, CpuHotAddEnabled, CpuHotRemoveEnabled
$Result | Export-Csv -NoTypeInformation $LogFile
Write-Host "Completed" -Foregroundcolor "Green"
}
Else
{
Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}
VM CPU and Memory Hot Add - This Add HotPlug
#Create this file in Scripts folder as per below C:\scripts\csv\csvfile.csv and save as .ps1
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)
}
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)
}
$vmlist = Import-CSV C:\scripts\csv\csvfile.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
Stop-VM -VM $vmname -RunAsync
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
$cpu = $item.cpu
$mem = [int]$item.mem * 1024
Enable-MemHotAdd $vmname
Enable-vCpuHotAdd $vmname
Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -RunAsync -Confirm:$false
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
Start-VM -VM $vmname -RunAsync
}
I hope you find this useful. Please amend to suite your environment and share the knowledge.
#Create a .ps1 C:\scripts\HotAddHotPlugStatus.ps1
#Import-Module VMware.VimAutomation.Core
Import-Module VMware.VimAutomation.PCloud
$VCServer = Read-Host 'Enter VC Server name'
$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)
$LogFile = "VMHotAddHotPlugStatus_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
$Result = @()
If($? -Eq $True)
{
Write-Host "Connected" -Foregroundcolor "Green"
Write-Host "Collecting Hot Plug Status of the VMS ..." -Foregroundcolor "Yellow" -NoNewLine
$Results = @()
$Result = (Get-VM | select ExtensionData).ExtensionData.config | Select Name, MemoryHotAddEnabled, CpuHotAddEnabled, CpuHotRemoveEnabled
$Result | Export-Csv -NoTypeInformation $LogFile
Write-Host "Completed" -Foregroundcolor "Green"
}
Else
{
Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red"
}
###
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)
}
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)
}
$vmlist = Import-CSV C:\scripts\csv\csvfile.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
Stop-VM -VM $vmname -RunAsync
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
$cpu = $item.cpu
$mem = [int]$item.mem * 1024
Enable-MemHotAdd $vmname
Enable-vCpuHotAdd $vmname
Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -RunAsync -Confirm:$false
}
foreach ($item in $vmlist) {
$vmname = $item.vmname
Start-VM -VM $vmname -RunAsync
}
This would be more useful if we could download the script or copy the text.
Try now was testing a wordpress plugin for a colleague and accidently forgot to turn it off
This method works great for up to vcenter 6.5 but it doesn’t seem to work for 6.7. Any ideas how to get this to work for version 6.7?
I haven’t tested this on my 6.7 lab yet. I will try and update
Please can you modify the script to run on 6.7. Basically the script should perfrom guest os shutdown >> Enable hog plug and hot add CPU and memory >> Start the Vm. Server Vmlists to go as input in txt or csv format
I currently do not have 6.7. the lab is upgraded to version 7.x. I will review again and update accordingly
Do a Get-Member on that [VMware.Vim.VirtualMachineConfigSpec] object. in some ESX versions, the method is named ReconfigVM() and others it’s named ReconfigVM_Task()