-
Notifications
You must be signed in to change notification settings - Fork 6
/
Backup-ESXiConfigs.ps1
45 lines (26 loc) · 1.09 KB
/
Backup-ESXiConfigs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Function Backup-ESXiConfigs {
param (
[ValidateNotNullOrEmpty()]
[string]
$BackupLocation,
[ValidateNotNullOrEmpty()]
[int]
$FileRotation,
[ValidateNotNullOrEmpty()]
[string]
$Server
)
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction Stop
Connect-VIServer -Server $Server
TRY {
GET-VMHOST | ForEach-Object {
$ESXiBak = "$BackupLocation\$($_.name)"
IF (-not(Test-path $ESXiBak)) {MKDIR $ESXiBak}
WHILE (((Get-ChildItem $ESXiBak).count) -gt $FileRotation) {Get-ChildItem $ESXiBak | Sort-Object lastwritetime | select -First 1 | Remove-Item -Force -Confirm:$false}
Get-VMHostFirmware -VMHost $_.name -BackupConfiguration -DestinationPath $ESXiBak
Get-ChildItem $ESXiBak | Sort-Object lastwritetime | select -Last 1 | Rename-Item -NewName "$(get-date -Format yyyy-MM-dd)_$($_.name).tgz"
}
} CATCH {
Write-Error $_.Exception -ErrorAction Continue
} Finally {Disconnect-VIServer -Confirm:$false}
}