Blog

Powercli script to enable hot add

Here is a simple script to enable hot add for virtual servers, here it shows adding it for one server independently and then below I’ll show you how to change hot add for multiple servers, at this stage I am assuming you have a connection to vCenter already connect-viserver…

In order for this script to work the VM will need to be powered down.

 

Function EnableMemHotAdd($vm)
{
$vm = get-vm "TEST02"
$vmtest = $vm | get-view
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.MemoryHotAddEnabled = $true
$vmConfigSpec.CPUHotAddEnabled = $true
$vmtest.reconfigVM($vmConfigSpec)
}

Now I have adapted the script to run for multiple machines, as mentioned above unfortunately the feature won’t allow you to change the option while the server is powered on, and won’t allow you to change the feature pending a restart. Therefor I wrote the script to target multiple servers listed in a text document and set up a scheduled task to run the powercli script out of hours.

Because this script will be as a Scheduled task, the task will call on Powershell so I want to add the PowerCli snapin, and then I want to add the connect to vCenter string. The file variable is obviously the location of the text doc that contains all of the VMs, there must be a file in this location with the same name stated in the variable, the file then contains all of the machines required to have the settings changed with their names listed as they are in vCenter. We then need a foreach statement to tell the script that the code inside the bracket needs to be run for each VM listed in the text document. The script will then find the shut the VM down without requiring a prompt, wait 60 seconds before moving onto the next command (this is because if the setting is applied whilst shutting down it will fail) it then finds the object, changes the setting and powers the VM back on.

 add-pssnapin VMware.VimAutomation.Core

connect-viserver -server "MyvCenterName" -username MyUser -Password VirtuallyHardware

$file = "C:\Scripts\Input\vms.txt"
$vms = get-content $file
foreach ($vm in $vms)

{
shutdown-vmguest $vm -confirm:$false
start-sleep -Seconds 60
$vmv = get-view (Get-VM $vm).ID
$vmConfigSpec.MemoryHotAddEnabled = $true
$vmConfigSpec.CPUHotAddEnabled = $true
$vmv.reconfigVM($vmConfigSpec)
start-vm $vm -confirm:$false
}

2 Comments

  1. Johna620

    Great article post.Really thank you! Fantastic. edaddeeekaed

  2. Smithd634

    Hello! This post could not be written any better! Reading through this post reminds me of my good ebegkdgbcagfgcee

Leave a Comment

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