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
}
vmname | cpu | mem |
LAB-IISCORE01 | 2 | 8 |
LAB-IISCORE02 | 2 | 8 |
LAB-DC03 | 2 | 4 |
LAB-SECURITY01 | 4 | 16 |
LAB-KEMP01 | 1 | 2 |