After creating the Restart-VMs.ps1 script, I also created a script to schedule vHardware changes. With this script, you’re able to upgrade or downgrade the amount of memory and or the amount of vCPU’s of a VM. Just lik the restart script, you can schedule this script and change the recommended parameters.
These are the parameters you need to supply to the script. The first two parameters are mandatory. When you supply all the parameters, you are able to add/remove Memory and add/remove vCPU’s.
-vCenter | enter the vCenter server you want to connect to. It’s possible to connect to multiple vCenter servers. |
-vmName | enter the name of the VM you want |
-MemoryMB | enter the amount of memory you want to add or remove from the VM. Be sure you enter the amount in MB. |
-MemoryOption | Supply the option add or remove. |
-CPUCount | Enter the amount of vCPU’s you want to add or remove from the VM. |
-CPUOption | Supply the option add or remove. |
This is how the script works.
- Connect to vCenter
- Shutdown the selected VM
- Add/remove memory and or vCPU’s
- Poweron the selected VM
- Disconnect vCenter
Now it’s time for an example. I have a VM called NAGIOS and needed to remove 512MB RAM and needed to add an extra vCPU. So I started the script with the following parameters:
.\Change-VM_Memory_CPU_Count.ps1 -vCenter vc01.ict-freak.local -vmName NAGIOS -MemoryMB 512 -MemoryOption Remove -CPUCount 1 -CPUOption Add
The VM is powered off and the memory is downgraded to 1024MB RAM and the total number of vCPU’s upgraded to 2.
You can download the script below.
The script:
################################################################### # # Change-VM_Memory_CPU_Count.ps1 # # -MemoryMB the amount of Memory you want # to add or remove from the VM in MB # -MemoryOption Add/Remove # -CPUCount the amount of vCPU's you want # to add or remove from the VM # -CPUOption Add/Remove # # Example: # .\Change-VM_Memory_CPU_Count.ps1 -vCenter vc01 # -vmName NAGIOS -MemoryMB 512 -MemoryOption Add # -CPUCount 1 -CPUOption Remove
#
# Version 1.0 May 2010 Arne Fokkema www.ict-freak.nl @afokkema
# #################################################################### param( [parameter(Mandatory = $true)] [string[]]$vCenter, [parameter(Mandatory = $true)] [string]$vmName, [int]$MemoryMB, [string]$MemoryOption, [int]$CPUCount, [string]$CPUOption ) function PowerOff-VM{ param([string] $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( [string] $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([string]$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([string]$vmName, [int]$NumCPU, [string]$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
Another great script. Thanks. Works great.
Is there a parameter that can be used to allow this script to run on next reboot?
Does anyone how this can be modified in order to modify multiple VMs?
Wow, hours upon hours trying to make this work. I might as well just wake up at 4AM, make the change on a VM from 2GB RAM to 4GB RAM and go back to sleep.
really helped a lot for our Automation Usecase!!! Thanks anyways.. Keep rocks
Excellent script! Thank you!
Thanks , can this be extended to all the VM’s under a particular folder.. I’ve a requirement to modify memory to 2G on nearly 200 VM’s which are orgianally allocated as 10G…. please help
Get-VM | ForEach {
$vmName=$_.VM
… the whole shebang-script here…
}
Ho can i get this script scheduled in windows 2008r2. I can’t get this done
When i run it in windows (from start -run) like this: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile C:\Progra~2\VMware\Infras~1\vSpher~1\vim.psc1 “& ‘C:\PS_Scripts\vmset.ps1’ -vCenter of-srv-vc-01.omnyacc.lan -vmName OF-DT-EXBH-01 -Memo
ryMB 768 -MemoryOption Remove”
I get an error:
Missing closing ‘)’ in expression.
At C:\PS_Scripts\vmset.ps1:30 char:5
+ <<<< [string[]]$vCenter,
+ CategoryInfo : ParserError: (CloseParenToken:TokenId) [], Parse
Exception
+ FullyQualifiedErrorId : MissingEndParenthesisInExpression
Can you please delete the machine name and vcentername in the script i provided before
Problem is solved
for versione 6 of CLI
function PowerOff-VM
Shutdown-VMGuest now use Stop-VMguest
Bye
hi, a question. this script change total vCPU but for edito number of socket and core? for a VM with more socket and core the script not work. need to specify numbero of core and total of vCPU..
Here is a quick method to use to make this happen: (VM has two sockets, three cores for six total vCPUs.)
$VMname= get-vm “VM1″
$TotalvCPU=6
$Cores=3
$spec=New-Object –Type VMware.Vim.VirtualMAchineConfigSpec –Property @{“NumCoresPerSocket” = $cores}
($VMname).ExtensionData.ReconfigVM_Task($spec)
$VMname | set-vm -numcpu $TotalvCPU
Love this script! Worked great,