VMware vSphere – How to script vMotion for your VMs

VMware vSphere – How to script vMotion for your VMs

VMware vMotion is a pretty good feature regarding the availability and load balancing in your vSphere environment. Today i created a vMotion script to help me create a backup with a backup software.

As so many times my blog posts are the result of a problem i had and for which i needed a solution. It shouldn’t be different today. I worked in my vSphere homelab. I created some virtual machines and installed my backup software of choice. My idea was to have a backup before doing any work with the VMs, just in case i screw it up. So i can easily go back to a known good state of the VM and try again. But this task wasn’t so easy.

As a vExpert, VMCE, MVP, Trainer or many other different tech people you can request a NFR license key for Veeam Availability Suite. So did i. The NFR key was delivered quickly to my mailbox, and was even faster installed in Veeam. But there was a catch. At least my NFR license is limited to two sockets, but with no limits for protected VMs, and it comes with a full 1-year retention period, instead of just 30 days as the regular trial.

So i had to deal with the fact that only one host (i’ve got three hosts in my lab with two sockets each) is protected by Veeam. This limitation woke the hunter in me because i had to find a solution. My goal was to backup all my VMs but with only two licensed sockets. The approach I chose was to set vSphere DRS to manual, then do a vMotion of all VMs to the host which helds the Veeam license, doing a backup and set DRS back to fully automated after backup. If you are working with ressource pools you shouldn’t disable DRS, because that results in removing the ressource pools. But there is a workaround for that too. Instead of creating a new problem i did the easy way and just set DRS to manual.

How to get the vMotion script

If you’re familiar with GitHub you can download my script from there:

https://github.com/driftar/vSphere

For any other user i’ll provide the script directly here:

[code lang=”powershell” gutter=”true”]
# .SYNOPSIS
# This script will start a vMotion of all virtual machines on a specified datastore to a specified ESXi host.
# If you are working with a backup software which is licensed to a specific host, # this will probably help you.
# Only recommended in smaller environments or if you have enough ressources on this host.

# .DESCRIPTION
# The script loads a PSSnapin; it sets some PowerCLI options; it connects to your vCenter Server with the given credentials;
# it gets all your VMs in an array; it starts then a Host vMotion of all the VMs in the array to a specified ESXi host.

# .NOTES
# File Name : pre-backup.ps1
# Version: : 1.0
# Author : Karl Widmer (info@)
# Prerequisite : PowerShell V2 over Vista and upper / VMware PowerCLI 6
# Tested on: : Windows Server 2012 R2
# with PowerCLI : PowerCLI 6.3 Release 1 build 3737840
# with PowerShell: 4.0
# Copyright 2016 – Karl Widmer / driftar’s Blog (www.)

# .LINK
# Script posted over: https://www.driftar.ch

# Load PowerCLI cmdlets
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue"

# Set PowerCLI behaviour regarding invalid certificates and deprecation warnings
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -DisplayDeprecationWarnings:$false -confirm:$false

# Define vCenter User and target Datastore
$vcHost = ‘vcenter.domain.com’
$vcUser = ‘administrator@domain.com’
$vcPass = ‘password’
$datastore = ‘your_datastore’
$cluster = ‘your_cluster’
$targetHost = Get-VMHost -Name yourhost.domain.com

# Connect to vCenter
Connect-VIServer $vcHost -User $vcUser -Password $vcPass

# Get VMs (pass array of VMs to $VMs, for example ‘get-datastore test | get-vm’)
$VMs = Get-Datastore $datastore | get-vm

# Get Cluster information to set DRS to Manual for backup window
Set-Cluster $cluster -DrsAutomationLevel Manual -Confirm:$false

Foreach($vm in $vms) {
Write-Host ("Start Host vMotion for VM ‘" + $VM.Name + "’")

Move-VM -VM (Get-VM -Name $vm) -Destination (Get-Vmhost $targethost) -RunAsync

Write-Host ("Waiting…")

Write-Host ("Host vMotion for VM ‘" + $VM.Name + "’ finished")
}

# This last script step should probably be executed in a post-backup script step.
# It sets the DRS automation level back to fully automated. Your VMs will then probably load-balance on your hosts.

# Set DRS on cluster back to FullyAutomated after backup window
Set-Cluster $cluster -DrsAutomationLevel FullyAutomated -Confirm:$false
[/code]

Update 07.11.2016

After updating my ESXi hosts to 6.0.0 Build 4510822 my script stopped working. So i simplified the script and released version 2.0.