Backup All ESXi Configurations with VMware PowerCLI

Rebuilding a ESXi host can take a long time especially if you have a complicated environment. VMware uses Host Profiles however this feature is all the way at the top of the licensing with Enterprise Plus. There is an alternative if you know a little Powershell and install VMware’s PowerCLI you can use Powershell style command to interact with VMware vCenter.

This script simply gets each ESXi host from the vCenter server and then runs the backup configuration command against it. This will generate a file for each of your ESXi servers that can be restored using the Set-VMHostFirmware command in PowerCLI. This script requires that VMware PowerCLI is installed on the workstation where it is executed. It will prompt you for credentials to login on the vCenter server at the beginning of execution. Be sure to update the $vCenter variable with your vCenter address.

Code

# This script initiates VMware PowerCLI
. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
#
# Specify vCenter Server, vCenter Server prompts for username and vCenter Server user password
$vCenter="vcenter.domain.local"

# Get local execution path
$localpath = Get-Location

Write-Host "Connecting to vCenter Server $vCenter" -foreground green
Connect-viserver $vCenter -WarningAction 0

# Get list of all ESXi hosts known by vCenter
$AllVMHosts =  Get-VMHost

ForEach ($VMHost in $AllVMHosts)
{
    Write-Host " "
    Write-Host "Backing Up Host Configuration: $VMHost" -foreground green
    Get-VMHostFirmware -VMHost $VMHost -BackupConfiguration -DestinationPath $localpath
}

Write-Host
Write-Host "Files Saved to: $localpath";
Write-Host
Write-Host "Press any key to close ..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Console Output

D:\Powershell\vSphere> & '.\Backup All ESXi Configurations.ps1'
          Welcome to the VMware vSphere PowerCLI!

Log in to a vCenter Server or ESX host:              Connect-VIServer
To find out what commands are available, type:       Get-VICommand
To show searchable help for all PowerCLI commands:   Get-PowerCLIHelp
Once you've connected, display all virtual machines: Get-VM
If you need more help, visit the PowerCLI community: Get-PowerCLICommunity

       Copyright (C) 1998-2012 VMware, Inc. All rights reserved.


Connecting to vCenter Server vcenter.domain.local

Name                           Port  User
----                           ----  ----
vcenter.domain.local                 443   root

Backing Up Host Configuration: vmhost-1.domain.local

Url  : http://vmhost-1.domain.local/downloads/configBundle-vmhost-1.domain.local.tgz
Data : D:\Powershell\vSphere\configBundle-vmhost-1.domain.local.tgz
Host : vmhost-1.domain.local


Backing Up Host Configuration: vmhost-2.domain.local

Url  : http://vmhost-2.domain.local/downloads/configBundle-vmhost-2.domain.local.tgz
Data : D:\Powershell\vSphere\configBundle-vmhost-2.domain.local.tgz
Host : vmhost-2.domain.local


Backing Up Host Configuration: vmhost-3.domain.local

Url  : http://vmhost-3.domain.local/downloads/configBundle-vmhost-3.domain.local.tgz
Data : D:\Powershell\vSphere\configBundle-vmhost-3.domain.local.tgz
Host : vmhost-3.domain.local


Files Saved to: D:\Powershell\vSphere

Press any key to close ...

References