So a colleague asked me. Is it possible to increase the memory and cpu sizes of a bunch of VMs using powershell? My answer was everything is possible with powercli so I told him let me have a look for you. First of all you can only increase memory or vCpu of a VM if hot plug is enabled. Without that, you will need to shutdown the VM and apply these changes.
## Load Powershell and import the VMware modules needed for below
Import-Module vmware.vimautomation.core
Get-Module -ListAvailable VMware* | Import-Module | Out-Null
## Connect to the vCenter or vCenters of the VMs to make these changes to
Connect-VIServer uk3p-vc01.lab.local, ntcp-vc01.lab.local
## Once Connected Set your variables for the VMs ## vCSA 1
$vCSA1 = "DTCP-PRGT01","DTCP-SRM01","DTCP-AUTODEPLOY01","UK3P-WEB01","UK3P-WEB02","DTCP-DC01"
## Once Connected Set your variables for the VMs ## vCSA 2
$vCSA2 = "HDCP-CONNSVR01","HDCP-CONNSVR02","HDCP-COMPOSER01","HDCP-THINAPP","HDCP-WEB01","HDCP-WEB02", "HDCP-SECURITY01"
## Verify the VMs matches the once you have specified above
get-vm $vCSA1 | select Name, PowerState, MemoryGB, NumCPU, VMhost, Version | ft -a
get-vm $vCSA2 | select Name, PowerState, MemoryGB, NumCPU, VMhost, Version | ft -a
### Once Happy, proceed to below
## Increase $VCSA1 and $VCSA2 VMs memory and cpu ###
get-vm $vCSA1 | set-vm -memoryGB 4 -NumCpu 2 -Confirm:$false
Start-Sleep -Seconds 3
get-vm $vCSA2 | set-vm -memoryGB 4 -NumCpu 2 -Confirm:$false
Write-Host "!!!VM Specs Configurations Complete!!!" -ForegroundColor Green
##### End of Script #########
You can alternatively use a csv and call it using specified Variables. I will cover this in a later post.
## Load Powershell and import the VMware modules needed for below
Import-Module vmware.vimautomation.core
Get-Module -ListAvailable VMware* | Import-Module | Out-Null
## Connect to the vCenter or vCenters of the VMs to make these changes to
Connect-VIServer uk3p-vc01.lab.local, ntcp-vc01.lab.local
## Once Connected Set your variables for the VMs ## vCSA 1
$vCSA1 = "DTCP-PRGT01","DTCP-SRM01","DTCP-AUTODEPLOY01","UK3P-WEB01","UK3P-WEB02","DTCP-DC01"
## Once Connected Set your variables for the VMs ## vCSA 2
$vCSA2 = "HDCP-CONNSVR01","HDCP-CONNSVR02","HDCP-COMPOSER01","HDCP-THINAPP","HDCP-WEB01","HDCP-WEB02", "HDCP-SECURITY01"
## Verify the VMs matches the once you have specified above
get-vm $vCSA1 | select Name, PowerState, MemoryGB, NumCPU, VMhost, Version | ft -a
get-vm $vCSA2 | select Name, PowerState, MemoryGB, NumCPU, VMhost, Version | ft -a
### Once Happy, proceed to below
## Increase $VCSA1 and $VCSA2 VMs memory and cpu ###
get-vm $vCSA1 | set-vm -memoryGB 4 -NumCpu 2 -Confirm:$false
Start-Sleep -Seconds 10
get-vm $vCSA2 | set-vm -memoryGB 4 -NumCpu 2 -Confirm:$false
Write-Host "!!!VM Specs Configurations Complete!!!" -ForegroundColor Green
##### End of Script #########
#### Updated Script ####
$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 16 –NumCpu 4 –Confirm:$False
$vm_name | Start-VM
}
## Above will export VMs with name "*Windows*" to a csv and the a shutdwon request will be issued changing vMem to 16 and Numcpu to 4
Thanks mate for sharing.