-
Notifications
You must be signed in to change notification settings - Fork 2
/
hyperv_backup.ps1
20 lines (16 loc) · 1.17 KB
/
hyperv_backup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## hyperv_backup.ps1
## Version 1.4
## Basically, it exports all VMs that have notes reading Auto-Backuped-Up then it compresses the exports, then moves the archive to a Samba share and cleans up the local backups.
## Thanks to Karl Glennon for the following 7-Zip function
## https://stackoverflow.com/questions/1153126/how-to-create-a-zip-archive-with-powershell
function create-7zip([String] $aDirectory, [String] $aZipfile){
[string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe";
[Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory"; & $pathToZipExe $arguments;
}
$todayDate = $((get-date).tostring('yyy-MM-dd_HH-mm'))
$hostname = Invoke-Command {hostname.exe}
get-vm | where-object { $_.Notes -like "*Auto-Backuped-Up*" } | Export-VM -Path F:\auto_hyperv_backups\$todayDate\
create-7zip "F:\auto_hyperv_backups\$todayDate\" "F:\auto_hyperv_backups\$todayDate.zip"
new-item -ItemType directory -Force -Path "\\houqnap01\IT\Backups\VMs\auto_hyperv_backups\$hostname\"
move-item "F:\auto_hyperv_backups\$todayDate.zip" -destination "\\houqnap01\IT\Backups\VMs\auto_hyperv_backups\$hostname\" -Force
remove-item -recurse "F:\auto_hyperv_backups\$todayDate"