PowerCLI – Disable/Enable hotadd cpu and memory options on multiple virtual machines

Hot Add Memory and Hot Add vCPU options in VMware lets you to add Memory and vCpu to virtual machine while the vm is powered-on however there might be situations where we would like to disable this option on virtual machines due to critical applications running on those VMs.

To disable Hot Add Memory and Hot Add vCPU options on virtual machine we have to manually edit each virtual machine settings after shutdown, this task can be performed easily using Power-CLI script however once the script is executed we need to power-off(shutdown) and then Power-On the vm to see the changes on Virtual machine.

1. Save the VM names in VMnames.txt in folder where you copy this script.
2. After you execute this script it will ask you for the vCenter name or ESXi host to connect.
3. You need to power-off(shutdown) and power-on the vm to see the effect of changes made through this script.
4. The script will look for the vm names supplied int he vmnames.txt file and performs the change only if that vm is present on the vCenter or ESXi host.

Note: Same script can be used to enable hotadd memory and vCpu options, only thing you need to do is change $extra.Value=”false”  to true in both the functions.

Script content PSC-DisableHMVM.ps1:

—————————————————————————————————————————————————–
#Function to disable hotadd memory
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)
}
#Function to disable hotadd vCpu
Function Disable-CPUHotAdd($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)
}

$ErrorActionPreference = “SilentlyContinue”
$vcname = Read-host “Enter the vCenter or Esxi host name to connect”
Connect-viserver $vcname
foreach ($computers in Get-Content “VMnames.txt”)
{
$vm = Get-VM $computers
If($vm -match $computers)
{
Disable-MemHotAdd ($vm)
Disable-CPUHotAdd ($vm)
write-host “Changed the hot add memory and cpu option on vm $vm ”
}
else
{
write-host ” $computers virtual machine is not present in vCenter”
}
}
Disconnect-viserver $vcname

—————————————————————————————————————————————————–

how to run the script

 

Advertisements

About asifkhandevadi

Hello, I have been working on windows since 9 years and currently working as windows, VMware and MS clustering SME at IBM. Whenever I get free time I participate in Microsoft forums and write some blogs to enhance my technical and communication skills through knowledge sharing. Please contact me on FB or Linkedin if you need any assistance on troubleshooting, implementation and virtualizaton.
This entry was posted in PowerCLI and tagged , , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s