added another host and logic to test if host is online

Without test for host online or other logic to see if backup was successful, the rename code would rename the host folder as the filename variable is blank
This commit is contained in:
2023-03-09 10:56:17 -05:00
parent 5f8e37dfed
commit e066cc3c31

View File

@@ -11,45 +11,49 @@ $esxiHosts = @(
"gauntesxi03.home.johnhgaunt.com" "gauntesxi03.home.johnhgaunt.com"
"gauntesxi04.home.johnhgaunt.com" "gauntesxi04.home.johnhgaunt.com"
"gauntesxi05.home.johnhgaunt.com" "gauntesxi05.home.johnhgaunt.com"
"gauntesxi06.home.johnhgaunt.com"
) )
# Loop through each host # Loop through each host
foreach ($esxiHost in $esxiHosts) { foreach ($esxiHost in $esxiHosts) {
#New-VICredentialStoreItem -host $esxiHost -user root -pass ((get-credential).GetNetworkCredential().password); continue #New-VICredentialStoreItem -host $esxiHost -user root -pass ((get-credential).GetNetworkCredential().password); continue
# Backup folder for each host # Only move forward if host is online or else the rename will rename the host folder
$backupFolder = "\\gauntnas.home.johnhgaunt.com\veeam$\ESXi Host Configurations\$esxiHost" if (Test-Connection -Count 1 -Quiet $esxiHost) {
# Backup folder for each host
$backupFolder = "\\gauntnas.home.johnhgaunt.com\veeam$\ESXi Host Configurations\$esxiHost"
# Create the backup folder # Create the backup folder
new-item -ItemType Directory -Path $backupFolder -ErrorAction SilentlyContinue new-item -ItemType Directory -Path $backupFolder -ErrorAction SilentlyContinue
# Get the credentials for the host # Get the credentials for the host
$credentials = Get-VICredentialStoreItem -Host $esxiHost $credentials = Get-VICredentialStoreItem -Host $esxiHost
# Connect to the host # Connect to the host
Connect-VIServer -server $esxiHost ` Connect-VIServer -server $esxiHost `
-user $credentials.user ` -user $credentials.user `
-Password $credentials.password -Password $credentials.password
# Backup the host configuration # Backup the host configuration
$filename = (Get-VMHostFirmware -vmhost $esxiHost ` $filename = (Get-VMHostFirmware -vmhost $esxiHost `
-BackupConfiguration ` -BackupConfiguration `
-destinationpath $backupFolder).Data.Name -destinationpath $backupFolder).Data.Name
# Rename the backup file with the date # Rename the backup file with the date
Rename-Item "$backupFolder\$filename" "$backupFolder\$(get-date -UFormat %Y%m%d-%H%M%S) $filename" -verbose Rename-Item "$backupFolder\$filename" "$backupFolder\$(get-date -UFormat %Y%m%d-%H%M%S) $filename" -verbose
# Only keep 25 backup files # Only keep 25 backup files
$daysToKeep = 25 $daysToKeep = 25
# Get old backup files # Get old backup files
$oldBackups = Get-ChildItem $backupFolder | ` $oldBackups = Get-ChildItem $backupFolder | `
Where-Object { -not $_.PsIsContainer } | ` Where-Object { -not $_.PsIsContainer } | `
Sort-Object CreationTime -Descending | ` Sort-Object CreationTime -Descending | `
Select-Object -Skip $daysToKeep Select-Object -Skip $daysToKeep
# Loop through each old file and remove it # Loop through each old file and remove it
foreach ($oldbackup in $oldBackups) { foreach ($oldbackup in $oldBackups) {
Remove-Item -Force $oldbackup.FullName -verbose Remove-Item -Force $oldbackup.FullName -verbose
} }
}
} }
#Stop-Transcript #Stop-Transcript