Knowledge Base

Helpful Info

A script to change a virtual machine's memory is helpful if you need to shut down the power.

We have found that sometimes we need to change the memory allocation to a virtual machine on the VM we are accessing the platform from. As such, you can run this script on another VM with access from vCenter and let it do the work.

			
				## Load Powershell and import the VMware modules needed for below
Import-Module VMware.VimAutomation.Core
Get-Module -ListAvailable VMware* | Import-Module | Out-Null
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

## Connect to the vCenter or vCenters of the VMs to make these changes to
$vcenter = Read-Host -Prompt 'Input your server  name'
$User = Read-Host -Prompt 'Input the user name'
$Cluster = Read-Host -Prompt 'Input the Cluster'
$ResPool = Read-Host -Prompt 'Input the Resource Pool'
$Mem = Read-Host -Prompt 'Input the Memory GB'

Connect-VIServer $vcenter -User $User

[array]$vms = Get-ResourcePool -Name $ResPool -Location (Get-Cluster $cluster) | Get-VM
foreach ($vm in $vms){

##Shut Machine Down
If($vm.powerstate -eq "PoweredOn"){
$vm | Shutdown-VMGuest –Confirm:$False

Do {
sleep -Seconds 5
$Status = (Get-vm $vm).powerstate
If($status -eq "PoweredOff")
{"$vm has been powered off adding hardware"}
else{
"$vm is still powered on looping until off"
}
}
While ($Status -eq "PoweredOn")
}
## Displaying Current RAM
Echo "Old Amount"
$vm | select Name, MemoryGB | ft -a
Echo "New Amount"

get-vm $vm | set-vm -memoryGB $Mem -Confirm:$false | select Name, MemoryGB | ft -a
}			
		

The script above is from MS tech articles on the internet and has been modified for our use. It comes with no warranty.