Other‎ > ‎

Adding Memory and Enabling MemoryHotAdd

posted Jul 12, 2013, 11:18 AM by Kris Knight   [ updated Apr 3, 2015, 10:54 PM ]
I wrote this to configure an array of VMs that needed to have their memory upgraded to a requested amount however some of the VMs had MemoryHotAdd Enabled and some VMs already had the requested amount. This would automate the process with PowerCLI. I probably could have used a csv file instead of an array but as I am still somewhat new to PowerCLI and had already made the array of 50+ VMs I figured it best just stick with that I had.
 
$VMs = @("VM1","VM2","VM3")

$NewMem = 8192

Function Enable-MemHotAdd($VirtualM){
    $VMget = Get-VM $VirtualM
    $Spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $Spec.MemoryHotAddEnabled = $true
    $VMget.ExtensionData.ReconfigVM($Spec)
    }

ForEach ($VM in $VMs)
    {
        $gVM = Get-VM $VM
        If (($gVM.ExtensionData.Config.MemoryHotAddEnabled -eq $true) -and ($gVM.MemoryMB -lt $NewMem)){
            Set-VM -VM $VM –MemoryMB $NewMem –Confirm:$false
            }
        ElseIf (($gVM.ExtensionData.Config.MemoryHotAddEnabled -eq $false) -and ($gVM.MemoryMB -lt $NewMem)){
            Shutdown-VMGuest $gVM –Confirm:$false
            While ($gVM.PowerState –eq “PoweredOn”){
                Sleep –Seconds 1
                $gVM = Get-VM $VM
            }
            Set-VM -VM $VM –MemoryMB $NewMem –Confirm:$false
            Enable-MemHotAdd $VM
            Start-VM $VM –Confirm:$false
            }
        Else {Write "I did nothing to this VM. -----> $VM"}
    }
Comments