Script: How to Increase Multiple Virtual Machines VMs vCPU and vMem using a CSV and PowerCLI

This is an updated blog posted requested by one of the regular users of this blog. IMPORTANT NOTE – the script will recursively shutdown each VM defined in CSV (unless Hot Add is already enabled), apply the config then power each VM back on. So make sure you run this script in an outage window.

$cred​​ =​​ Get-Credential
$vCenters​​ =​​ "lab-vcenter01.lab.local",​​ "lab-vcenter02.lab.local"
Connect-VIServer​​ $vCenters​​ -Credential​​ $cred

$VMsToUpgrade = get-vm |?{$_.Name -like "*windows*"} | select name | export-csv -Path C:\Temp\VMsToUpgrade.csv

## Or You can use below hashed out 
## $Windows = "HDCP-CONNSVR01","HDCP-CONNSVR02","HDCP-COMPOSER01","HDCP-THINAPP","HDCP-WEB01","HDCP-WEB02", "HDCP-SECURITY01"
##$VMsToUpgrade = get-vm $windows | select name | export-csv -Path C:\Temp\VMsToUpgrade.csv

$vm_name = (Import-Csv -Path C:\Temp\VMsToUpgrade.csv).Name
ForEach ($vm in $vm_name){
$vm_name | Shutdown-VMGuest –Confirm:$False
Sleep 60
$vm_name | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False
$vm_name | Start-VM
}

## Also, if you want to set different vCPU and vMem as per spreadsheet below. You can do so by following script

$vmlist = Import-CSV C:\temp\VMsToUpgrade.csv
foreach ($item in $vmlist) {
    $vmname = $item.vmname
    $cpu = $item.cpu
    $mem = [int]$item.mem
    Set-VM -VM $vmname -NumCpu $cpu -MemoryGB $mem -RunAsync -Confirm:$false
}

If you want to know How to Set VM Hot Plug For Memory and CPU (vCPU and vMem Hot Plug) Using PowerCLI , refer to this link on a previous post

vmnamecpumem
LAB-IISCORE0128
LAB-IISCORE0228
LAB-DC0324
LAB-SECURITY01416
LAB-KEMP0112

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

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