PowerCli to Increase Hardware on Multiple VMs

The other night I had a task of upgrading 22 VMs with additional vCPUs and memory.  I also had a maintenance window of 60 minutes to get this completed.  So I wasn’t going to do this more than once, so I just wrote a simple PowerCli script that would handle this process for me.


$servers = "VMServer01","VMServer02","VMServer03","VMServer04","VMServer05"

foreach ($server in $servers){
Shutdown-VMGuest $server -confirm:$false
Do {
sleep -Seconds 5
$Status = (Get-vm $server).powerstate
If($status -eq "PoweredOff")
{"$Server has been powered off adding hardware"}
else{
"$server is still powered on looping until off"
}
}
While ($Status -eq "PoweredOn")

$TotalvCPU=6
$Cores=1
$VMname = get-vm $server
$spec=New-Object –Type VMware.Vim.VirtualMAchineConfigSpec –Property @{“NumCoresPerSocket” = $cores}
($VMname).ExtensionData.ReconfigVM_Task($spec)

$VMname | Set-VM -MemoryGB 28 -NumCpu $TotalvCPU -Confirm:$false

Sleep -Seconds 5
Start-VM $server -Confirm:$false
}

So this script starts off identifying the servers that the work is going to be performed on. Then it will power the VM off using Shutdown-VMGuest.  I put a mechanism in to perform a loop while the VM is turning off.  Once the VM is powered off, the script will then perform the upgrade to the CPU and Memory.  Then the server gets started up.

Once the script was completed, I then used the $servers variable to pull a list of the VMs with the current vCPU and memory assigned by

Get-vm $servers

This displays generated the following:

This is the output from Get-VM using the variable $servers

– Stuart

1 thought on “PowerCli to Increase Hardware on Multiple VMs”

  1. Since this use of VirtualMAchineConfigSpec pops up in a lot of search results, I just wanted to add that nowadays Set-VM has gained a simple -CoresPerSocket parameter.

    As an example, this one-liner now works:

    Get-VM -Name xxx | Set-VM -NumCpu 2 -CoresPerSocket 2

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.