Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

How to Set VM Hot Plug For Memory and CPU (vCPU and vMem Hot Plug) Using PowerCLI – VirtuallyThatGuy

Posted on 26 April 20186 December 2022 by VirtuallyThatGuy

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

    }

 

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
    }


9 thoughts on “How to Set VM Hot Plug For Memory and CPU (vCPU and vMem Hot Plug) Using PowerCLI – VirtuallyThatGuy”

  1. TOM BOGAN says:
    13 July 2019 at 21:52

    This would be more useful if we could download the script or copy the text.

    Reply
    1. VirtuallyThatGuy says:
      27 July 2019 at 22:25

      Try now was testing a wordpress plugin for a colleague and accidently forgot to turn it off

      Reply
  2. David says:
    6 August 2019 at 18:38

    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?

    Reply
    1. VirtuallyThatGuy says:
      14 September 2019 at 23:39

      I haven’t tested this on my 6.7 lab yet. I will try and update

      Reply
      1. Sadasiba Rout says:
        26 October 2021 at 17:54

        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

        Reply
        1. VirtuallyThatGuy says:
          27 October 2021 at 10:01

          I currently do not have 6.7. the lab is upgraded to version 7.x. I will review again and update accordingly

          Reply
  3. Daniel Petcher says:
    30 August 2020 at 23:08

    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()

    Reply
  4. Pingback: Script: Increase Multiple Virtual Machines VMs vCPU and vMem using a CSV and PowerCLI - VirtuallyThatGuy
  5. Pingback: Script: How to Increase Multiple Virtual Machines VMs vCPU and vMem using a CSV and PowerCLI - VirtuallyThatGuy

Leave a Reply Cancel reply

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

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme