VMware Cloud Community
vmware12
Enthusiast
Enthusiast
Jump to solution

Help with Script for Hot Add CPU/Mem for large number of VMs

So I found these two functions from

It

appears to be only for a single VM at a time, yet I need to configure

them to do several hundred servers. Is there a way to implement

foreach ($vm in $devVMs)

$devVMs = c:\listofvms.txt

into

the script to point to a list of VMs, like a list of Windows 2008 64bit

VMs, Windows 2003 32bit vms as another list etc instead of doing one VM

at a time.

The two functions are for Hot Add Memory and CPU functionality per VM OS.

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)

}

Any help is greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can save that script in a .ps1 file.

After you connect to the vCenter (Connect-VIServer) you run the script from the PowerCLI prompt.

.\myscript.ps1

It will read all the guestnames you stored in the .txt file and for each of these call the function.

If you need to tackle another bunch of guests you change the contents of the .txt file and execute the script again.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Perhaps I misunderstood the message, but can't you do

Function Enable-vCpuAndMemHotAdd($vm){
  $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $extra1 = New-Object VMware.Vim.optionvalue
  $extra1.Key="mem.hotadd"
  $extra1.Value="true"
  $vmConfigSpec.extraconfig += $extra1
  $extra2 = New-Object VMware.Vim.optionvalue
  $extra2.Key="vcpu.hotadd"
  $extra2.Value="true"
  $vmConfigSpec.extraconfig += $extra2
  $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Get-Content  "c:\listofvms.txt" | %{
   $vm = Get-VM -Name $_
   Enable-vCpuAndMemHotAdd $vm 
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmware12
Enthusiast
Enthusiast
Jump to solution

So forgive me but I am very new still to the PowerCLI Front. How would I call that? Would I just set that up as a script to run on its own, then execute it from the powerCLI window and it would take the listofvms and run through them changing the values of the CPU/MEM hot add functionality, then just change the listofvms.txt and run it again? Or do I need to paste this command into PowerCLI then run a command like:

<code>Enable-vCpuAndMemHotAdd testvm1</code>

 To get this to run? 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can save that script in a .ps1 file.

After you connect to the vCenter (Connect-VIServer) you run the script from the PowerCLI prompt.

.\myscript.ps1

It will read all the guestnames you stored in the .txt file and for each of these call the function.

If you need to tackle another bunch of guests you change the contents of the .txt file and execute the script again.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmware12
Enthusiast
Enthusiast
Jump to solution

Thank you so much for the help, nice blog too.

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

When I am running your script I have error:

You cannot call a method on a null-valued expression.

At :line:11 char:30

+ $vm.Extensiondata.ReconfigVM <<<< ($vmConfigSpec)

Do you know how I can fix it?

Thanks a lot,

qwert

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion

Could be that the version you're using doesn't know the Extensiondata property.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc,

You are right (as always)! I used old version and as soon as I upgraded to latest it start working just fine.

I might have one more question for you later (if I will not resolve it myself).

Thank you very much!!!

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Luc/guys,

The script is working great! thanks a lot!

It does changing settings for "Enable CPU/Memory hot add/plug" on live VMs.

However, I noticed that I still cannot adjust (add) memory till VMs are powered off and powered back on.  Is something wrong on my host/VC?

I guess, something needs to be done on VM level in order to this settings take affect, could it be done through scripting?

Thanks a lot

0 Kudos
cblomart
Enthusiast
Enthusiast
Jump to solution

doesn't a vm need to be restarted to enable hotadd? i know it seems strang to reboot to hot add... afterwards you can add cpu without restart...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is correct, the VM needs to be restarted.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
qwert1235
Enthusiast
Enthusiast
Jump to solution

Thanks a lot for clarifying this for me!

qwert

0 Kudos
lledarby
Contributor
Contributor
Jump to solution

Hi Luc,

I want to use this script to apply to all VM's in the Clusters. I modified the call to be:

%{
$vm = Get-VM
Get-Cluster |  Enable-vCpuandMemHotAdd
}

I keep getting an error:

You cannot call a method on a null-valued expression.

At C:\Scripts\hotadd.ps1:11 char:31

+   $vm.Extensiondata.ReconfigVM <<<< ($vmConfigSpec)

    + CategoryInfo          : InvalidOperation: (ReconfigVM:String) [], Runtim

   eException

    + FullyQualifiedErrorId : InvokeMethodOnNull

I've tried powercli versions 5.0 build 435427 and 4.1 U1 build 332441

Any advice?

The complete script is:

Function Enable-vCpuAndMemHotAdd($vm){

  $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

  $extra1 = New-Object VMware.Vim.optionvalue

  $extra1.Key="mem.hotadd"

  $extra1.Value="true"

  $vmConfigSpec.extraconfig += $extra1

  $extra2 = New-Object VMware.Vim.optionvalue

  $extra2.Key="vcpu.hotadd"

  $extra2.Value="true"

  $vmConfigSpec.extraconfig += $extra2

  $vm.Extensiondata.ReconfigVM($vmConfigSpec)

}

%{

$vm = Get-VM

Get-Cluster | Enable-vCpuandMemHotAdd

0 Kudos