Mass Reconfigure of Virtual Machine vCPU Sockets/Cores using VMware PowerCLI

powercli

Increasing the amount of vCPUs in ESXi is a simple admin task when working with a single or low number of virtual machines. However, performing this on a large number of virtual machines can be an intensive task.

This article is about my use case and how I resolved this with a script, which can be found at the bottom.

Use Case

In my case, a customer was expanding their VDI cluster and increasing virtual machine vCPU capacity from 2 to 4. Besides of this, the configuration required a 2 socket – 2 core (4 vCPU) setup.

The script I wrote for this, assumes the virtual machines you want to modify are powered off and modifies all virtual machines on the selected ESXi host. Of course you can modify the parameters to have a selection based on virtual machine name or to exclude specific virtual machines.

The script logs any actions to the $LogFile file, for historical/troubleshooting references.

Make sure you test the script on a non-production environment first. I am not responsible for any negative outcomes by using this script.

Script


#Import PowerCLI commandlets
Import-Module VMware.PowerCLI

#Parameters - Customizable
$vCenterServer = "vcenter.fqdn.com" #This is the vCenter Server instance you want to work with
$ESXHost = "esxi.fqdn.com" #Use the ESXi host name as shown in vSphere Web Client
$LogFile = "D:\Temp\BulkSetVcpu.log"

#Logging
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | Out-Null

$ErrorActionPreference = "Continue"
Start-Transcript -path $LogFile -append

#Make sure no existing vCenter Server connections are active
Write-Host "Disconnecting any existing vCenter Server connections..."
Disconnect-VIServer * -Force -Confirm:$false

#Connect to vCenter Server
Write-Host "Connecting to $vCenterServer..."
Connect-VIServer $vCenterServer

#Get current VM configuration for logging purposes
Write-Host "Getting current VM configuration"
Get-VMhost $ESXHost | Get-VM | FT

#Get Power-On VMs (as they will not be able to change vCPU configuration while running)
Write-Host "These VMs are currently not powered off and will not be reconfigured"
Get-VMhost $ESXHost | Get-VM | Where {$_.PowerState -notmatch "PoweredOff"} | FT

#Setting new vCPU settings for powered off VMs
Write-Host "Reconfiguring powered off VMs with 4 vCPUs"
$VMSelection = Get-VMhost $ESXHost | Where {$_.PowerState -match "PoweredOff"}

#Start configuring Permissions
Write-Host "Configuring new vCPUs..."
ForEach ($VM in $VMSelection)

              {

                $VMSpec = New-Object -Type VMware.Vim.VirtualMAchineConfigSpec -Property @{"NumCoresPerSocket" = 2}
                $VM.ExtensionData.ReconfigVM_Task($VMSpec)
                Get-VM $VM | Set-VM -NumCPU 4 -Confirm:$false

              }

#Disconnect from vCenter Server
Write-Host "Disconnecting from $vCenterServer..."
Disconnect-VIServer * -Force -Confirm:$false

#Stop Logging
Stop-Transcript

Leave a Reply

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