Taking ESXi Config Backup using PowerShell
Script to back up all your ESXi hosts in your vCenter environment
My test code:
function LoadSnapin{
param($PSSnapinName)
if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){
Add-pssnapin -name $PSSnapinName
}
}
# Load PowerCLI snapin
LoadSnapin -PSSnapinName "VMware.VimAutomation.Core"
# Variables
[string] $vCenter = "192.168.131.20"
[string] $BackupPath = "C:\test"
# Connect to vCenter
Connect-VIServer $vCenter -user "vmadmin" -Pass "vmware@123" -Protocol Https
# Get All Connected Hosts
$VMH = Get-VMHost | Sort Name
# For each Host in All Connected Hosts
Foreach ($eVMH in $VMH)
{
# Backup Host config
Set-VMHostFirmware -VMHost $eVMH.name -BackupConfiguration -DestinationPath $BackupPath
}
# Disconnect from Host
Disconnect-VIServer -Confirm:$false
Results:
Your code should be like below:
function LoadSnapin{
param($PSSnapinName)
if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){
Add-pssnapin -name $PSSnapinName
}
}
# Load PowerCLI snapin
LoadSnapin -PSSnapinName "VMware.VimAutomation.Core"
# Variables
[string] $vCenter = <<”your vCenter IP”>
[string] $BackupPath = "C:\test"
# Connect to vCenter
Connect-VIServer $vCenter -user <<”Your user name”>> -Pass <<”your password”>> -Protocol Https
# Get All Connected Hosts
$VMH = Get-VMHost -Location RMGADC | Sort Name
# For each Host in All Connected Hosts
Foreach ($eVMH in $VMH)
{
# Backup Host config
Set-VMHostFirmware -VMHost $eVMH.name -BackupConfiguration -DestinationPath $BackupPath
}
# Disconnect from Host
Disconnect-VIServer -Confirm:$false