Hi All,
We have an environment with 100s of VMs. We have classified these VMs and each category will have specific vCPU and vMem. An excel / csv sheet has the list of VMs along with the vCPU and vMem values . I am looking for a script that can set the vCPU and vMem values for the VMs as per the excel sheet. Can someone help with this?
Many Thanks
Try this. I took the hotadd function from here:
http://ict-freak.nl/2009/10/05/powercli-enabledisable-the-vm-hot-add-features/
I tested against 1 VM and it works ok. it will loop and power all VM's off in parallel, then it will modify them all. THen power them all on in parallel. If you don't want parallel you can modify it.
I would test in your lab or against 1-2 non prod VM's first.
Same CSV colum
VMNAME,MEM,CPU
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:\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
}
you could do this with the column headers being:
VMName,CPU,MEM
$vmlist = Import-CSV C:\csvfile.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$cpu = $item.cpu
$mem = [int]$item.mem * 1024
Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -RunAsync -Confirm:$false
}
Thanks for that. I have this cmdlet. But I need the script to enable the HotAdd for vCPU and vMem and then perform the Set-VM values. Should have made it specific, sorry.
Best Regards
It will require the VM's to be brought offline is that fine?
Yes, Absolutely. As long as the script can shutdown the VMs, ensure that the hotadd is enabled and set the vCPU and Mem values, bring up the VMs, it would be great.
Try this. I took the hotadd function from here:
http://ict-freak.nl/2009/10/05/powercli-enabledisable-the-vm-hot-add-features/
I tested against 1 VM and it works ok. it will loop and power all VM's off in parallel, then it will modify them all. THen power them all on in parallel. If you don't want parallel you can modify it.
I would test in your lab or against 1-2 non prod VM's first.
Same CSV colum
VMNAME,MEM,CPU
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:\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 left out -Confirm:$false on the start-vm and stop-vm cmdlets . You will want to add that in order to not be prompted
I would use the Shutdown-VMGuest cmdlet whenever it's possible (VMware Tools installed) instead of Stop-VM, especially on production VMs.
Good call on that Eco, that would be a better option to use. Wasn't actually familiar with that cmdlet
Good call indeed. I will mention here though, what I just learned... that 'Shutdown-VMguest' has been replaced by 'Stop-VMguest' in PowerCLI 6 up.