VMware Cloud Community
ANSUDHARSON
Contributor
Contributor
Jump to solution

Script to set the CPU and Memory for VMs

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

1 Solution

Accepted Solutions
markdjones82
Expert
Expert
Jump to solution

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
    }
http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com

View solution in original post

0 Kudos
9 Replies
markdjones82
Expert
Expert
Jump to solution

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

}

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
ANSUDHARSON
Contributor
Contributor
Jump to solution

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

0 Kudos
markdjones82
Expert
Expert
Jump to solution

It will require the VM's to be brought offline is that fine? 

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
ANSUDHARSON
Contributor
Contributor
Jump to solution

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.

0 Kudos
markdjones82
Expert
Expert
Jump to solution

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
    }
http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
markdjones82
Expert
Expert
Jump to solution

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

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
EKardinal
Enthusiast
Enthusiast
Jump to solution

I would use the Shutdown-VMGuest cmdlet whenever it's possible (VMware Tools installed) instead of Stop-VM, especially on production VMs.

markdjones82
Expert
Expert
Jump to solution

Good call on that Eco, that would be a better option to use.  Wasn't actually familiar with that cmdlet

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
slanger
Enthusiast
Enthusiast
Jump to solution

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.

VMware vSphere PowerCLI 6.0 Release 1 Release Notes

vSphere 6.0 Documentation Center

0 Kudos