add scrubber back into deployment script

This commit is contained in:
2026-10-29 15:05:37 -04:00
parent 3e24920646
commit 52aa59a70c
5 changed files with 283 additions and 38 deletions
+149 -14
View File
@@ -389,7 +389,7 @@ try {
Assert-OfficeTest (($script:mediaProcesses -join ',') -eq 'odt.exe,setup.exe,setup.exe') 'Staging must only extract ODT and download Office, with no removal or installation.'
$removal = [xml](Get-Content -LiteralPath $media.RemovalConfig -Raw)
Assert-OfficeTest ($removal.Configuration.Remove.All -eq 'TRUE' -and $removal.Configuration.Display.Level -eq 'None') 'Staging must save the silent removal configuration.'
Assert-OfficeTest (!$media.Scrubber -and !($script:mediaSignatures -match 'GetHelp|SaRA')) 'Unattended deployments must not stage an interactive scrubber.'
Assert-OfficeTest (!$media.Scrubber -and !($script:mediaSignatures -match 'GetHelp|SaRA')) 'Ordinary media staging must not download the conditional scrubber.'
foreach ($mode in @('win10', 'incompatible-download', 'history-unavailable')) {
$mediaWorkspace = Join-Path $testRoot $mode; $null = [IO.Directory]::CreateDirectory($mediaWorkspace)
$options = Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft 365 Business Standard'; OfficeArchitecture='64'}
@@ -553,7 +553,9 @@ try {
[pscustomobject]@{DisplayName='Microsoft Office Home and Business 2021 - en-us'}
}
}
function Get-OfficeClickToRunState { [pscustomobject]@{Present=($script:removalMode -eq 'orphan')} }
function Get-OfficeClickToRunState { [pscustomobject]@{Present=($script:removalMode -eq 'orphan');SourceReferences=@();WorkingConfiguration=@($(if ($script:removalMode -eq 'stale') {'old-guid'}))} }
function Get-OfficeRemovalCorruptionEvidence { if ($script:removalMode -eq 'corrupt') { [pscustomobject]@{Path='fixture.log';Detail='REMOVEINSTALLATION InvalidDirectory'} } }
function Invoke-OfficeScrubRecovery { param($Media,$Workspace,$Reason) $script:scrubCalls++; Assert-OfficeTest (![string]::IsNullOrWhiteSpace($Reason)) 'Recovery must record its reason.'; return 3010 }
function Invoke-OfficeProcess {
param($FilePath,$Arguments,$LogPrefix)
$script:removalCalls++; $script:removalStarted=$true
@@ -564,25 +566,26 @@ try {
$removalPath = Join-Path $testRoot 'remove [literal].xml'
(New-OfficeRemovalConfiguration ([pscustomobject]@{OfficeAppShutdown='Force'})).Save($removalPath)
$media = [pscustomobject]@{Setup='inert-setup.exe'; RemovalConfig=$removalPath}
foreach ($script:removalMode in @('empty','orphan','msi-only','mixed','delayed','remaining','read-error','fatal','reboot')) {
$script:removalChecks=0; $script:removalCalls=0; $script:removalStarted=$false
$script:removalCode = switch ($script:removalMode) { 'fatal' {1603}; 'reboot' {3010}; default {0} }
foreach ($script:removalMode in @('empty','orphan','msi-only','mixed','delayed','remaining','stale','corrupt','unknown-17004','read-error','fatal','reboot')) {
$script:removalChecks=0; $script:removalCalls=0; $script:removalStarted=$false; $script:scrubCalls=0
$script:removalCode = switch ($script:removalMode) { 'fatal' {1603}; 'reboot' {3010}; 'corrupt' {17004}; 'unknown-17004' {17004}; default {0} }
if ($script:removalMode -eq 'remaining') {
Assert-OfficeThrows { Remove-OfficeSuite $media $testRoot } 'Office remains.*Office Home and Business 2021.*manual scrub'
Assert-OfficeTest ((Remove-OfficeSuite $media $testRoot) -eq 3010) 'Verified leftovers after ODT success must select scrub recovery.'
Assert-OfficeTest ($script:removalChecks -eq ((Get-OfficeDeploymentDefinitions).RegistrationCheckAttempts + 2)) 'Remaining Click-to-Run products must be checked for the bounded registration interval.'
} elseif ($script:removalMode -eq 'read-error') {
Assert-OfficeThrows { Remove-OfficeSuite $media $testRoot } 'Office inventory is unreadable'
} elseif ($script:removalMode -eq 'fatal') {
Assert-OfficeThrows { Remove-OfficeSuite $media $testRoot } 'ODT Office removal failed \(exit 1603\).*No interactive scrubber was launched.*manual scrub'
} elseif ($script:removalMode -in @('fatal','unknown-17004')) {
Assert-OfficeThrows { Remove-OfficeSuite $media $testRoot } 'ODT Office removal failed.*automatic scrub was not selected'
} else {
$result = @(Remove-OfficeSuite $media $testRoot)
$expected = if ($script:removalMode -eq 'reboot') {3010} else {0}
$expected = if ($script:removalMode -in @('reboot','corrupt','stale')) {3010} else {0}
Assert-OfficeTest ($result.Count -eq 1 -and $result[0] -eq $expected) 'Removal must return just its exit code; MSI products are deferred to installation.'
if ($script:removalMode -eq 'delayed') { Assert-OfficeTest ($script:removalChecks -eq 4) 'Removal must tolerate registry propagation delays.' }
if ($script:removalMode -eq 'reboot') { Assert-OfficeTest ($script:removalChecks -eq 1) 'A required restart must be surfaced before demanding registration cleanup.' }
}
$expectedCalls = if ($script:removalMode -in @('empty','msi-only')) {0} else {1}
Assert-OfficeTest ($script:removalCalls -eq $expectedCalls) 'Remove orphaned Click-to-Run state even without registered products, without rerunning failed removal or launching a fallback scrubber.'
Assert-OfficeTest ($script:removalCalls -eq $expectedCalls) 'Remove orphaned Click-to-Run state even without registered products, without rerunning ODT.'
Assert-OfficeTest ($script:scrubCalls -eq $(if ($script:removalMode -in @('corrupt','remaining','stale')) {1} else {0})) 'Only confirmed removal corruption or leftovers may trigger a single scrub.'
}
}
& {
@@ -834,6 +837,123 @@ try {
Assert-OfficeTest ((Get-Content -LiteralPath $Global:LogFile -Raw) -match 'Error: User cleanup task registration failed for unmapped \(C:\\Users\\defaultuser100000\)') 'Registration failures must reach the Tools diagnostic log.'
}
# Classify only current, removal-specific corruption; stale errors in a live
# service log and generic 17004 wrappers must not authorize a destructive scrub.
& {
$path=Join-Path $testRoot 'Office-corruption.log'
function Get-OfficeSetupLogFiles { Get-Item -LiteralPath $path }
$since=[DateTime]::UtcNow.AddSeconds(-2)
$stamp=[DateTime]::Now.ToString('M/d/yyyy H:mm:ss.fff', [Globalization.CultureInfo]::InvariantCulture)
$detail='OFFICECL Click-To-Run Non Task Error {"ErrorCode":30034,"ErrorType":"InvalidDirectory","ErrorMessage":"GetFiles: search path root\\integration does not exist","Task":"REMOVEINSTALLATION:{A1D863B9-4F69-45AF-BD5C-D349E5C0C851}"}'
Set-Content -LiteralPath $path -Value "$stamp`t$detail"
$evidence=Get-OfficeRemovalCorruptionEvidence $since
Assert-OfficeTest ($evidence.Path -eq $path -and $evidence.Detail.Contains('root\\integration')) 'The observed InvalidDirectory removal error must trigger recovery evidence.'
foreach ($line in @(
('10/22/2026 13:57:00.000' + "`t" + $detail),
("$stamp`t" + $detail.Replace('REMOVEINSTALLATION', 'INSTALL')),
("$stamp`t" + $detail.Replace('InvalidDirectory', 'AccessDenied')),
("$stamp`tSETUP unknown error [ExitCode=17004]"),
('No timestamp ' + $detail),
([DateTime]::Now.AddHours(1).ToString('M/d/yyyy H:mm:ss.fff', [Globalization.CultureInfo]::InvariantCulture) + "`t" + $detail)
)) {
Set-Content -LiteralPath $path -Value $line
Assert-OfficeTest ($null -eq (Get-OfficeRemovalCorruptionEvidence $since)) 'Old, unrelated, unparseable and future evidence must not select recovery.'
}
function Get-Content { throw 'Log locked' }
Assert-OfficeTest ($null -eq (Get-OfficeRemovalCorruptionEvidence $since)) 'Unreadable evidence must not select recovery.'
}
# Download/extract only on demand, validate the Microsoft executable, and never
# rely on Tools.ps1's shadowed Expand-Archive or a previously extracted package.
& {
Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
$script:scrubPackageMode='single'; $script:scrubSignatures=@(); $script:scrubDownloads=0
function Save-OfficeDownload {
param($Uri,$Path)
Assert-OfficeTest ($Uri -eq (Get-OfficeDeploymentDefinitions).GetHelpUrl) 'Recovery must use the current Microsoft download link.'
$script:scrubDownloads++
$zip=[IO.Compression.ZipFile]::Open($Path, [IO.Compression.ZipArchiveMode]::Create)
try {
$entries = switch ($script:scrubPackageMode) {
'traversal' { @('../escaped.exe') }
'missing' { @('readme.txt') }
'duplicate' { @('one/GetHelpCmd.exe','two/GetHelpCmd.exe') }
default { @('GetHelpCmd.exe','support.dll') }
}
foreach ($name in $entries) { $entry=$zip.CreateEntry($name); $writer=[IO.StreamWriter]::new($entry.Open()); try { $writer.Write('Inert archive fixture, never executed.') } finally { $writer.Dispose() } }
} finally { $zip.Dispose() }
}
function Assert-OfficeMicrosoftSignature { param($Path) $script:scrubSignatures += $Path; if ($script:scrubPackageMode -eq 'unsigned') { throw 'Invalid Microsoft signature' } }
function Expand-Archive { throw 'Must use the literal-path Archive module, not the Tools override.' }
foreach ($script:scrubPackageMode in @('single','missing','duplicate','traversal','unsigned')) {
$workspace=Join-Path $testRoot ('scrub package [' + $script:scrubPackageMode + ']'); $null=[IO.Directory]::CreateDirectory($workspace)
if ($script:scrubPackageMode -eq 'single') {
$exe=Initialize-OfficeScrubMedia $workspace
Assert-OfficeTest ($exe -eq (Join-Path $workspace 'GetHelp\GetHelpCmd.exe') -and $exe -in $script:scrubSignatures) 'Freshly extracted Get Help must pass signature verification.'
$before=$script:scrubDownloads
Assert-OfficeThrows { Initialize-OfficeScrubMedia $workspace } 'refusing to reuse'
Assert-OfficeTest ($script:scrubDownloads -eq $before) 'A second recovery must not download or reuse its package.'
} elseif ($script:scrubPackageMode -eq 'traversal') {
Assert-OfficeThrows { Initialize-OfficeScrubMedia $workspace } 'outside the Office deployment root'
Assert-OfficeTest (!(Test-Path -LiteralPath (Join-Path $workspace 'escaped.exe'))) 'ZIP extraction must not escape its folder.'
} elseif ($script:scrubPackageMode -eq 'unsigned') { Assert-OfficeThrows { Initialize-OfficeScrubMedia $workspace } 'Invalid Microsoft signature' }
else { Assert-OfficeThrows { Initialize-OfficeScrubMedia $workspace } 'exactly one GetHelpCmd.exe' }
}
}
# A scrub requires both a successful scenario/process result and independently
# verified removal. Every attempt is bounded to one and cleanup retains its logs.
& {
$script:scrubLaunches=0
function Initialize-OfficeScrubMedia {
param($Workspace)
Set-Content -LiteralPath (Join-Path $Workspace 'GetHelp.zip') -Value 'fixture'
if ($script:scrubMode -eq 'download') { throw 'Scrub download failed' }
Join-Path $Workspace 'GetHelpCmd.exe'
}
function Invoke-OfficeProcess {
param($FilePath,$Arguments,$LogPrefix)
$script:scrubLaunches++
Assert-OfficeTest ($Arguments -eq '-S OfficeScrubScenario -AcceptEula -OfficeVersion All' -and (Split-Path -Leaf $LogPrefix) -eq 'office-scrub') 'Scrub must use documented unattended arguments with separate logs.'
Set-Content -LiteralPath "$LogPrefix.stdout.log" -Value $script:scrubOutput
Set-Content -LiteralPath "$LogPrefix.stderr.log" -Value ''
$script:scrubExit
}
function Get-CimInstance { if ($script:scrubMode -eq 'running') { [pscustomobject]@{Name='GetHelpCmd.exe'} } }
function Get-OfficeSuiteInventory {
if ($script:scrubMode -eq 'remaining') { [pscustomobject]@{DisplayName='Leftover MSI Office';WindowsInstaller=1} }
if ($script:scrubMode -eq 'inventory-error') { throw 'Registry unreadable' }
}
function Assert-OfficeFreshInstallState { if ($script:scrubMode -eq 'stale') { throw 'Previous Click-to-Run state remains' } }
foreach ($script:scrubMode in @('success','restart','download','failure','not-found-66','not-found-68','empty','contradictory','bad-exit','running','remaining','inventory-error','stale')) {
$workspace=Join-Path $testRoot ('scrub-result-' + $script:scrubMode); $null=[IO.Directory]::CreateDirectory($workspace)
$media=[pscustomobject]@{}; $script:scrubExit=0; $script:scrubOutput='00: Successfully completed this scenario.'
switch ($script:scrubMode) {
'restart' { $script:scrubExit=3010 }
'failure' { $script:scrubOutput='09: Failure to remove Office.' }
'not-found-66' { $script:scrubOutput='66: We could not find the specified Office version.'; $script:scrubExit=66 }
'not-found-68' { $script:scrubOutput='68: We could not find any Office version.' }
'empty' { $script:scrubOutput='' }
'contradictory' { $script:scrubOutput="09: Failure`n00: Success" }
'bad-exit' { $script:scrubExit=1 }
}
if ($script:scrubMode -in @('success','restart')) {
$result=@(Invoke-OfficeScrubRecovery $media $workspace 'fixture corruption')
Assert-OfficeTest ($result.Count -eq 1 -and $result[0] -eq 3010 -and $media.ScrubAttempted -and $media.ScrubReason -eq 'fixture corruption') 'Verified scrub must surface its use and restart requirement without leaking log messages.'
} else {
$pattern=switch ($script:scrubMode) { 'download' {'Scrub download failed'}; 'running' {'processes are still running'}; 'remaining' {'Office remains.*Leftover MSI Office'}; 'inventory-error' {'Registry unreadable'}; 'stale' {'Click-to-Run state remains'}; default {'failed or returned an unverified result'} }
Assert-OfficeThrows { Invoke-OfficeScrubRecovery $media $workspace 'fixture corruption' } $pattern
}
$before=$script:scrubLaunches
Assert-OfficeThrows { Invoke-OfficeScrubRecovery $media $workspace 'second attempt' } 'already attempted'
Assert-OfficeTest ($script:scrubLaunches -eq $before) 'Failure must not create a scrub loop.'
$warnings=Complete-OfficeDeploymentFiles $workspace
if ($script:scrubMode -eq 'running') { Assert-OfficeTest ($warnings.Count -gt 0 -and (Test-Path -LiteralPath (Join-Path $workspace 'GetHelp.zip'))) 'Surviving scrub workers must retain in-use media.' }
else {
Assert-OfficeTest (!$warnings.Count -and !(Test-Path -LiteralPath (Join-Path $workspace 'GetHelp.zip'))) 'Scrub download must be removed on success and failure.'
if ($script:scrubMode -ne 'download') { Assert-OfficeTest (Test-Path -LiteralPath (Join-Path $workspace 'office-scrub.stdout.log')) 'Scrub result logs must survive cleanup.' }
}
}
Assert-OfficeTest (Test-OfficeStagingInUse $testRoot @([pscustomobject]@{Name='cscript.exe';CommandLine='cscript.exe C:\Temp\OffScrubC2R.vbs ALL'})) 'Detached Office scrub scripts must block a new deployment and media cleanup.'
}
# Deployment resolves release metadata and deletes it on success or failure.
& {
$script:historyPaths=@()
@@ -866,11 +986,11 @@ try {
function Save-OfficeDownload { throw 'Assessment must not download data.' }
$options = Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft/Office 365 E3/E5 (no Teams)'}
$checks = @(Test-OfficeDownloadLinks $options)
Assert-OfficeTest ($checks.Count -eq 6 -and $checks.Count -eq $script:heads.Count) 'HEAD checks must cover ODT discovery, helper, CDN, and selected companions.'
Assert-OfficeTest ($checks.Count -eq 7 -and $checks.Count -eq $script:heads.Count) 'HEAD checks must cover ODT discovery, helper, recovery link, CDN, and selected companions.'
Assert-OfficeTest ((Get-OfficeDeploymentDefinitions).CompanionInstallers.Teams.Uri -notin $script:heads) 'No-Teams assessment must not probe the Teams download.'
$options.InstallTeams=$true; $options.OfficeFeatureRelease='2608'; $script:heads=@()
$checks = @(Test-OfficeDownloadLinks $options)
Assert-OfficeTest ($checks.Count -eq 8 -and (Get-OfficeDeploymentDefinitions).OfficeReleaseHistory -in $script:heads) 'Windows 10 assessment must check history availability without resolving the build.'
Assert-OfficeTest ($checks.Count -eq 9 -and (Get-OfficeDeploymentDefinitions).OfficeReleaseHistory -in $script:heads) 'Windows 10 assessment must check history availability without resolving the build.'
$script:headCode=204
Assert-OfficeThrows { Test-OfficeDownloadLinks $options } 'returned 204; expected 200'
$script:headFails=$true
@@ -1027,11 +1147,18 @@ try {
Set-Content -LiteralPath (Join-Path $profile 'NTUSER.DAT') -Value 'test hive, never loaded'
function Stop-OfficeDeploymentApps { $script:events += 'apps' }
function Get-OfficeRunningApps { }
function Get-OfficeSuiteInventory { if ($script:mode -ne 'office-reboot' -or $script:removalStillRegistered) { [pscustomobject]@{DisplayName='Synthetic Office'} } }
function Get-OfficeSuiteInventory { if ($script:mode -notin @('office-reboot','scrub-recovery') -or $script:removalStillRegistered) { [pscustomobject]@{DisplayName='Synthetic Office'} } }
function Get-OfficeMachineCompanionEntries { if ($script:removalStillRegistered) { [pscustomobject]@{DisplayName='Synthetic OneDrive'} } }
function Initialize-OfficeMedia { param($Options,$Workspace) $script:events += 'download'; if ($script:mode -in @('download','download-cleanup')) { throw 'download failure' }; [pscustomobject]@{Version='16.0.1.2'; Helper=(Join-Path $Workspace 'Office-UserCleanup.ps1')} }
function Remove-OfficeCompanionApps { $script:events += 'companions'; if ($script:mode -eq 'companions') { throw 'companion failure' }; return ($script:mode -eq 'reboot') }
function Remove-OfficeSuite { $script:events += 'remove-office'; if ($script:mode -eq 'remove-office') { throw 'Office removal failure' }; if ($script:mode -eq 'office-reboot') { return 3010 }; return 0 }
function Remove-OfficeSuite {
param($Media,$Workspace)
$script:events += 'remove-office'
if ($script:mode -eq 'remove-office') { throw 'Office removal failure' }
if ($script:mode -eq 'scrub-recovery') { $Media | Add-Member ScrubAttempted $true; $Media | Add-Member ScrubReason 'fixture recovery'; return 3010 }
if ($script:mode -eq 'office-reboot') { return 3010 }
return 0
}
function Clear-OfficeProfileIdentity { $script:events += 'identity'; if ($script:mode -eq 'identity') { throw 'identity failure' } }
function Start-OfficeUserCleanup {
$script:events += 'user'; if ($script:mode -eq 'user') { throw 'user still running' }
@@ -1063,6 +1190,14 @@ try {
if ($script:mode -eq 'office-reboot') { Assert-OfficeTest ($result.OfficeDeploymentStatus -eq 'RestartRequiredBeforeInstall' -and 'identity' -notin $script:events -and 'user' -notin $script:events) 'Office removal restart must stop before user cleanup and replacement installation.' }
if ($script:mode -eq 'success') { Assert-OfficeTest (($script:events -join ',') -eq 'download,apps,companions,remove-office,identity,user,phase,install,install-companions') 'Deployment order changed.' }
}
foreach ($deferScrub in @('true','false')) {
$script:mode='scrub-recovery'; $script:events=@()
$options=Get-OfficeDeploymentOptions @{OfficeSubscription='Microsoft 365 Business Premium';OfficeAppShutdown='Force';ValidateBeforeChanges='false';DeferReboot=$deferScrub}
$result=Invoke-OfficeDeployment $options
Assert-OfficeTest ($result.ExitCode -eq 3010 -and $result.ScrubRecoveryAttempted -and $result.ScrubRecoveryReason -eq 'fixture recovery') 'Scrub recovery and restart requirement must be visible in the final receipt.'
if ($deferScrub -eq 'true') { Assert-OfficeTest ($result.OfficeDeploymentStatus -eq 'Installed' -and 'install' -in $script:events) 'Verified scrub may continue installation when reboot is deferred.' }
else { Assert-OfficeTest ($result.OfficeDeploymentStatus -eq 'RestartRequiredBeforeInstall' -and 'install' -notin $script:events -and 'identity' -notin $script:events) 'Nondeferred scrub restart must stop before replacement installation and user cleanup.' }
}
$script:mode='preflight-space'; $script:events=@(); $options.ValidateBeforeChanges=$true; $script:workspaceCreates=0; $script:artifactCleanupRuns=0
Assert-OfficeThrows { Invoke-OfficeDeployment $options } 'Insufficient disk space before download'
Assert-OfficeTest ($script:events.Count -eq 0 -and $script:workspaceCreates -eq 0 -and $script:artifactCleanupRuns -eq 0) 'Low-space validation must stop without staging or housekeeping.'