From 606f831b539a6ab4d8875620894e5b725d920f4d Mon Sep 17 00:00:00 2001 From: SCUR0 Date: Fri, 6 Dec 2024 10:12:18 -0800 Subject: [PATCH] Update Backup-VM.ps1 More progress cleanup and fixes --- Tasks/Backup-VM.ps1 | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/Tasks/Backup-VM.ps1 b/Tasks/Backup-VM.ps1 index b7ccbf1..fdd4b55 100644 --- a/Tasks/Backup-VM.ps1 +++ b/Tasks/Backup-VM.ps1 @@ -19,6 +19,9 @@ Path for 7Zip. If left empty uses the default path for 7zip executable. If 7zip is not found the script will export to folders uncompressed. +.PARAMETER Password + 7zip Encryption password + .EXAMPLE Backup-VM.ps1 -ExportPath "J:\Backup\VMs" -Exclude "test-vm-1","test-vm-2" #> @@ -29,7 +32,7 @@ param ( $ExportPath, $Exclude, $ZipExe="C:\Program Files\7-Zip\7z.exe", - $Password + $Password ) if ($Name){ @@ -111,11 +114,13 @@ foreach ($VM in $VMs) { $CurrentCount++ } -Write-Progress -Activity "Backing up VM(s) ($($VMs.count))" -Completed - #Compress if 7zip is installed if ($Zip -and ($CompletedVMs)){ Write-Verbose "Compressing Exports" + + #use half of available threads + $MMT = ((Get-CimInstance Win32_Processor).NumberOfLogicalProcessors | Measure-Object -Sum).Sum / 2 + cd $ExportPath foreach ($CompletedVM in $CompletedVMs){ @@ -123,26 +128,19 @@ if ($Zip -and ($CompletedVMs)){ $Percent=[math]::Round($CurrentCount/$StepCount*100) Write-Progress -Activity $PTitle -Status "Compressing $CompletedVM" -PercentComplete $Percent -Id 1 $FileName = "$CompletedVM-$(Get-Date -Format "yy-MM-dd")" - Start-Process -FilePath $ZipExe -ArgumentList "a $FileName $CompletedVM $EncryptArgs -bsp1" -NoNewWindow -PassThru | ForEach-Object { - if ($_ -match '\S'){ - $String = ($_ | Out-String).Trim() - $CompPerc = ($String -split '%')[0] - if ($CompPerc -match '^[0-9]+$'){ - Write-Progress -Activity "Compressing" -Status "$CompPerc% Completed" -PercentComplete $CompPerc -Id 2 - } - } - } - Write-Progress -Activity "Compressing" -Id 2 -Completed + Start-Process -FilePath $ZipExe -ArgumentList "a $FileName $CompletedVM $EncryptArgs -mmt=$MMT -bsp1" -NoNewWindow -Wait + if (Test-Path "$FileName.7z"){ Write-Verbose "Deleting uncompressed files" remove-item -Recurse -Force $CompletedVM } $CurrentCount++ } + Write-Progress -Activity "Compressing" -Id 2 -Completed }else{ if (!$Zip){ Write-Output "7zip can be used to compress and archive VMs. Install 7zip or use custom install path in launch arguments." } } -Write-Progress -Activity $PTitle -Id 1 -Completed +Write-Progress -Activity $PTitle -Id 1 -Completed \ No newline at end of file