In the VMWare ESXi standard edition, there is a limitation when placing the server in maintenance mode. It is never complete unless you move all the VM from that host to another host and then place the server in maintenance mode.
You need to distribute the VMs based on the host which has more free memory and is also healthy.

For a medium-scale environment, this limitation needs to be addressed as this process takes a lot of the administrator’s time. Redistribute the load and check which server has more memory..etc, so I got the idea to automate this.

Prerequisits

  • PowerShell 5.1
  • VMWare Module Core to installed
  • Valid Network connection to vCenter host

Download and Parameters

The below script can be downloaded from GitHub and do the following steps:

PowerShell VMWare Maintenance Mode Script in Action

  • Import the VMWare module and connect to the VI Server.
  • Get a list of all the VM in the host that should be placed in maintenance mode.
  • For each virtual machine, find the proper server to place it (based on the most server which has the most free memory).
  • Redistribute the load.
    • if no server is available with enough memory to have the VM, the script will let you know and won’t overcommit your servers, unless you want so.
  • After all is done, will provide you with a basic report about each VM, the old host, and the new host.
  • place the server in maintenance mode.

Supported Parameters

The script requires some parameters to be filled in before it can do the magic:

  • [string]FromVMHostName: The name of the server you want to move the VM from and place it in maintenance mode, Please Note that the server name should be exactly as the one registered in your vCenter, so if you are using FQDN, pass it here too.
  • [Int]MaxMemAllowed: The Percentage of the total free memory of the destination server before being excluded from the selection, the default value is 80, so no VM will be shipped to any server which has more than 80 percent of utilized memory.
  • [string] vCenter: your vCenter IP or name
  • [string] FromCluster: the cluster you want to search through

Example

The following example gets all the VMs from myserver.domain.local and distributes them to all hosts in the same cluster which has less than 80 percent utilized memory. So this will help from overloading the servers.

Set-AutoEMM.ps1 -FromVMHostName myserver.domain.local -MaxMemAllowed 80 -FromCluster Production

What if and during the migration the server’s memory utilization reaches more than 80 percent? Well, the script will evaluate the server before and after each VM being moved and do the proper calculation. So before moving the VM the script calculate and check if the destination server will be over 80 percent, it will be excluded from being a destination for the migration.

If no more hosts are available with less than 80 percent memory utilization (or whatever the value you set in the MaxMemAllowed ), the script will fail and stop, surely it will display something on the screen telling that no more hosts are available.
Hope you enjoy this script and find it useful, please let me know by commenting or dropping me a message farisnt@gmail.com
If you notice any bug or issue, let me know 🙂

Rate this post