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.
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
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
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?
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
Thank you so much for the help, nice blog too.
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
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
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!!!
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
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...
That is correct, the VM needs to be restarted.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot for clarifying this for me!
qwert
Hi Luc,
I want to use this script to apply to all VM's in the Clusters. I modified the call to be:
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