61 lines
2.0 KiB
PowerShell
61 lines
2.0 KiB
PowerShell
# Set-PowerCLIConfiguration -DefaultVIServerMode Single
|
|
# Set-PowerCLIConfiguration -InvalidCertificateAction ignore
|
|
# Set-PowerCLIConfiguration -ParticipateInCEIP $false
|
|
|
|
#Start-Transcript "C:\Users\Veeam_Service\Desktop\Backup ESXi Host Configurations.log"
|
|
|
|
# make sure to stop on an error
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
# host array to backup
|
|
$esxiHosts = @(
|
|
"gauntesxi01.home.johnhgaunt.com"
|
|
"gauntesxi02.home.johnhgaunt.com"
|
|
"gauntesxi03.home.johnhgaunt.com"
|
|
)
|
|
|
|
# Loop through each host
|
|
foreach ($esxiHost in $esxiHosts) {
|
|
#New-VICredentialStoreItem -host $esxiHost -user root -pass ((get-credential -Message $esxiHost -UserName root).GetNetworkCredential().password); continue
|
|
|
|
# Only move forward if host is online or else the rename will rename the host folder
|
|
if (Test-Connection -Count 1 $esxiHost) {
|
|
# Backup folder for each host
|
|
$backupFolder = "\\gauntnas.home.johnhgaunt.com\veeam$\ESXi Host Configurations\$esxiHost"
|
|
|
|
# Create the backup folder
|
|
new-item -ItemType Directory -Path $backupFolder -ErrorAction SilentlyContinue
|
|
|
|
# Get the credentials for the host
|
|
$credentials = Get-VICredentialStoreItem -Host $esxiHost -Verbose
|
|
|
|
# Connect to the host
|
|
Connect-VIServer -server $esxiHost `
|
|
-user $credentials.user `
|
|
-Password $credentials.password `
|
|
-verbose
|
|
|
|
# Backup the host configuration
|
|
$filename = (Get-VMHostFirmware -vmhost $esxiHost `
|
|
-BackupConfiguration `
|
|
-destinationpath $backupFolder `
|
|
-verbose).Data.Name
|
|
|
|
# Rename the backup file with the date
|
|
Rename-Item "$backupFolder\$filename" "$backupFolder\$(get-date -UFormat %Y%m%d-%H%M%S) $filename" -verbose
|
|
|
|
# Only keep 25 backup files
|
|
$daysToKeep = 25
|
|
# Get old backup files
|
|
$oldBackups = Get-ChildItem $backupFolder | `
|
|
Where-Object { -not $_.PsIsContainer } | `
|
|
Sort-Object CreationTime -Descending | `
|
|
Select-Object -Skip $daysToKeep
|
|
# Loop through each old file and remove it
|
|
foreach ($oldbackup in $oldBackups) {
|
|
Remove-Item -Force $oldbackup.FullName -verbose
|
|
}
|
|
}
|
|
}
|
|
|
|
#Stop-Transcript |