From 39f583ff5aebf74df90d9da9b9ecf6164ba95514 Mon Sep 17 00:00:00 2001 From: Tu Dinh Date: Fri, 10 Jan 2025 20:32:51 +0100 Subject: [PATCH] scripts: Add cleanup code for Windows guests Signed-off-by: Tu Dinh --- scripts/guests/windows/install-autotest.ps1 | 23 ++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/guests/windows/install-autotest.ps1 b/scripts/guests/windows/install-autotest.ps1 index daabc144..bd623d46 100644 --- a/scripts/guests/windows/install-autotest.ps1 +++ b/scripts/guests/windows/install-autotest.ps1 @@ -1,7 +1,11 @@ +#Requires -RunAsAdministrator + [CmdletBinding()] param ( [Parameter()] - [switch]$NoNetReporting + [switch]$NoNetReporting, + [Parameter()] + [switch]$Cleanup ) $ErrorActionPreference = "Stop" @@ -52,6 +56,23 @@ if (!$NoNetReporting) { Register-ScheduledTask -InputObject $task -TaskName "XCP-ng Test Network Report" } +if ($Cleanup) { + Read-Host -Prompt "Unplug Internet, run Disk Cleanup and continue" + + Write-Output "Cleaning up component store" + dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase + + Write-Output "Cleaning up SoftwareDistribution" + Stop-Service wuauserv, BITS -ErrorAction SilentlyContinue + Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "$env:windir\SoftwareDistribution\Download\*" + + Write-Output "Cleaning up Defender signatures" + & "$env:ProgramFiles\Windows Defender\MpCmdRun.exe" -RemoveDefinitions -All + + Write-Output "Optimizing system drive" + defrag.exe $env:SystemDrive /O +} + Write-Output "Resealing" Stop-Process -Name sysprep -ErrorAction SilentlyContinue & "$env:windir\System32\Sysprep\sysprep.exe" /generalize /oobe /shutdown /unattend:$PSScriptRoot\unattend.xml