Skip to main content

As always run it on a test VM while you are watching it, but this script will power down, modify the CPU and/or Memory on a VM, then power it back up. 

 

This has been VERY handy for Changing resources on my VMs after hours. Like adding memory to a Veeam Virtual proxy server after a backup window ends. 

 

While I borrowed most of the script from the internet, there wasn’t much about running as a scheduled task which was can cause confusion to many people from what I read.  Here is a few tips to run the PowerShell script as a scheduled task. 

 

Step 1, Use the script below to create a PS1 file. 

  • I named mine VM_MEMORY_CPU_COUNT_HQ.PS1     Keep in mind the name and location when you add the arguments below.

Step 2, Create a scheduled task in windows at specified time.

 

Step 3, set action to run a program. You don’t want to run the script, you want to run Powershell.exe

example - C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

 

 

Step 4, Configure the options\arguments. I put 0 if I want no change on CPU or Memory. The flags Add or Remove need to be entered.  Use the numbers below to not get decimal numbers of memory. 

# 1GB = 1024
# 2GB = 2048
# 4GB = 4096
# 6GB = 6144
# 8GB = 8192

 

Real example of my arguments box in scheduled task. This would add 4GB of memory and not change any CPU. My script is in c:\Scripts\vmware\VM_MEMORY_CPU_COUNT_HQ.ps1. Yours may be different from step 1.

 

*VCENTERNAME* = Your vcenter

*SERVERNAME* = the VM you want to modify. 

 

-ExecutionPolicy Bypass -command "& C:\Scripts\vmware\VM_MEMORY_CPU_COUNT_HQ.ps1 -vCenter *VCENTERNAME* -vmName *SERVERNAME* -MemoryMB 4096 -MemoryOption Add -CPUCount 0 -CPUOption Add"

 

 

Step 5, Start in box for scheduled task. - I set this to the folder where my PS1 script is, but it doesn’t appear to be required. 

 

You will now enter your credentials when you hit OK. (hopefully your vCenter is AD joined with the appropriate permission) 

####################################################################

param(
#parameter(Mandatory = $true)]
estringn]]$vCenter,
>parameter(Mandatory = $true)]
estring]$vmName,
rint]$MemoryMB,
bstring]$MemoryOption,
int]$CPUCount,
bstring]$CPUOption
)

function PowerOff-VM{
param(cstring] $vm)

Shutdown-VMGuest -VM (Get-VM $vm) -Confirm:$false | Out-Null
Write-Host "Shutdown $vm"
do {
$status = (get-VM $vm).PowerState
}until($status -eq "PoweredOff")
return "OK"
}

function PowerOn-VM{
param( cstring] $vm)

if($vm -eq ""){ Write-Host "Please enter a valild VM name"}

if((Get-VM $vm).powerstate -eq "PoweredOn"){
Write-Host "$vm is already powered on"}

else{
Start-VM -VM (Get-VM $vm) -Confirm:$false | Out-Null
Write-Host "Starting $vm"
do {
$status = (Get-vm $vm | Get-View).Guest.ToolsRunningStatus
}until($status -eq "guestToolsRunning")
return "OK"
}
}

function Change-VMMemory{
param(nstring]$vmName, {int]$MemoryMB, (string]$Option)
if($vmName -eq ""){
Write-Host "Please enter a VM Name"
return
}
if($MemoryMB -eq ""){
Write-Host "Please enter an amount of Memory in MB"
return
}
if($Option -eq ""){
Write-Host "Please enter an option to add or remove memory"
return
}

$vm = Get-VM $vmName
$CurMemoryMB = ($vm).MemoryMB

if($vm.Powerstate -eq "PoweredOn"){
Write-Host "The VM must be Powered Off to continue"
return
}

if($Option -eq "Add"){
$NewMemoryMB = $CurMemoryMB + $MemoryMB
}
elseif($Option -eq "Remove"){
if($MemoryMB -ge $CurMemoryMB){
Write-Host "The amount of memory entered is greater or equal than
the current amount of memory allocated to this VM"
return
}
$NewMemoryMB = $CurMemoryMB - $MemoryMB
}

$vm | Set-VM -MemoryMB $NewMemoryMB -Confirm:$false
Write-Host "The new configured amount of memory is"(Get-VM $VM).MemoryMB
}

function Change-VMCPUCount{
param(Cstring]$vmName, {int]$NumCPU, astring]$Option)
if($vmName -eq ""){
Write-Host "Please enter a VM Name"
return
}
if($NumCPU -eq ""){
Write-Host "Please enter the number of vCPU's you want to add"
return
}
if($Option -eq ""){
Write-Host "Please enter an option to add or remove vCPU"
return
}

$vm = Get-VM $vmName
$CurCPUCount = ($vm).NumCPU

if($vm.Powerstate -eq "PoweredOn"){
Write-Host "The VM must be Powered Off to continue"
return
}

if($Option -eq "Add"){
$NewvCPUCount = $CurCPUCount + $NumCPU
}
elseif($Option -eq "Remove"){
if($NumCPU -ge $CurCPUCount){
Write-Host "The number of vCPU's entered is higher or equal
than the current number of vCPU's allocated to this VM"
return
}
$NewvCPUCount = $CurCPUCount - $NumCPU
}

$vm | Set-VM -NumCPU $NewvCPUCount -Confirm:$false
Write-Host "The new configured number of vCPU's is"(Get-VM $VM).NumCPU
}

#######################################################################################
# Main script
#######################################################################################

$VIServer = Connect-VIServer $vCenter
If ($VIServer.IsConnected -ne $true){
Write-Host "error connecting to $vCenter" -ForegroundColor Red
exit
}

if($MemoryMB -or $CPUCount -ne "0"){
$poweroff = PowerOff-VM $vmName
if($poweroff -eq "Ok"){
Write-Host "PowerOff OK"

if($MemoryMB -ne "0"){
if($MemoryOption -eq " ") {Write-Host "Please enter an option to add or remove memory"}
else{
Change-VMMemory $vmName $MemoryMB $MemoryOption
}
}

if($CPUCount -ne "0"){
if($CPUOption -eq " ") {Write-Host "Please enter an option to add or remove cpu"}
else{
Change-VMCPUCount $vmName $CPUCount $CPUOption
}
}

$poweron = PowerOn-VM $vmName
if($poweron -eq "Ok"){
Write-Host "PowerOn OK"}
}
}

Disconnect-VIServer -Confirm:$false

 

Really love scripts like this that make the Admins job easier.  Thanks for sharing it as I have one very similar that I was using.


Thank you @Scott for sharing it here 😃, very nice and well done. 


Comment