fixed Install-MicrosoftOffice365.ps1

added Repair-OneDriveKnownFolderPaths.ps1
This commit is contained in:
2026-10-22 00:16:40 -04:00
parent aea80c7be4
commit 031cfe40a1
8 changed files with 1041 additions and 22 deletions
+64 -7
View File
@@ -118,6 +118,24 @@ try {
}
}
}
& {
# Personal Teams has a different process name from both classic and current Teams.
# Exercise detection and both shutdown choices without touching real processes.
function Get-Process {
param($Name,$ErrorAction)
foreach ($candidate in @('Teams','msteams','ms-teams','unrelated')) {
if ($candidate -in $Name) { [pscustomobject]@{ProcessName=$candidate; Id=123; HasExited=$false} }
}
}
function Stop-Process { param($InputObject,[switch]$Force,$ErrorAction) $script:stoppedTeams += $InputObject.ProcessName }
$script:stoppedTeams=@()
$running = @(Get-OfficeRunningApps)
Assert-OfficeTest ($running.Count -eq 3 -and 'msteams' -in $running.ProcessName -and 'unrelated' -notin $running.ProcessName) 'Running-app detection must include personal Teams without broad process matching.'
Assert-OfficeThrows { Stop-OfficeDeploymentApps ([pscustomobject]@{OfficeAppShutdown='Abort'}) } 'Apps are running:.*msteams'
Assert-OfficeTest ($script:stoppedTeams.Count -eq 0) 'Abort must not close any Teams client.'
Stop-OfficeDeploymentApps ([pscustomobject]@{OfficeAppShutdown='Force'})
Assert-OfficeTest ($script:stoppedTeams.Count -eq 3 -and 'msteams' -in $script:stoppedTeams) 'Force must close the older personal Teams client along with classic/current Teams.'
}
& {
function Get-OfficeRunningApps { [pscustomobject]@{Id=123; HasExited=$script:appHasExited} }
function Stop-Process { throw 'Process is gone or access was denied.' }
@@ -306,7 +324,8 @@ try {
function Get-AppxProvisionedPackage {
param([switch]$Online,$ErrorAction)
if ($script:appMode -eq 'delayed-success') { $script:packageChecks[$script:lastInstalledApp]++; if ($script:packageChecks[$script:lastInstalledApp] -lt 3) { return } }
if ($script:appMode -ne 'missing-Teams') { [pscustomobject]@{DisplayName='MSTeams'; PackageName='MSTeams_1.0.0.0_neutral_~_8wekyb3d8bbwe'; Version='1.0.0.0'} }
if ($script:appMode -eq 'personal-Teams-only') { [pscustomobject]@{DisplayName='MicrosoftTeams'; PackageName='MicrosoftTeams_1.0.0.0_neutral_~_8wekyb3d8bbwe'; Version='1.0.0.0'} }
elseif ($script:appMode -ne 'missing-Teams') { [pscustomobject]@{DisplayName='MSTeams'; PackageName='MSTeams_1.0.0.0_neutral_~_8wekyb3d8bbwe'; Version='1.0.0.0'} }
if ($script:appMode -ne 'missing-NewOutlook') { [pscustomobject]@{DisplayName='Microsoft.OutlookForWindows'; PackageName='Microsoft.OutlookForWindows_1.0.0.0_neutral_~_8wekyb3d8bbwe'; Version='1.0.0.0'} }
}
function Get-OfficeMachineOneDrive {
@@ -315,7 +334,7 @@ try {
}
function Assert-OfficeMicrosoftSignature { }
$media = @{WebView2='WebView2'; OneDrive='OneDrive'; Teams='Teams'; NewOutlook='NewOutlook'}
foreach ($script:appMode in @('success','delayed-success','reboot','fail-WebView2','fail-OneDrive','fail-Teams','fail-NewOutlook','missing-OneDrive','missing-Teams','missing-NewOutlook')) {
foreach ($script:appMode in @('success','delayed-success','reboot','fail-WebView2','fail-OneDrive','fail-Teams','fail-NewOutlook','missing-OneDrive','missing-Teams','missing-NewOutlook','personal-Teams-only')) {
$script:webViewPresent = $false; $script:appCalls = @(); $failure = $null; $result = $null
$script:webViewChecks=0; $script:oneDriveChecks=0; $script:packageChecks=@{}
try { $result = Install-OfficeCompanionApps $media $testRoot -InstallTeams $true } catch { $failure = $_ }
@@ -326,6 +345,7 @@ try {
Assert-OfficeTest (($script:appCalls -join '|') -eq 'WebView2 /silent /install|OneDrive /allusers /silent|Teams -p|NewOutlook --provision true --quiet --start-') 'Companion installers must run silently and for all users.'
} else {
Assert-OfficeTest ($null -ne $failure) "Companion failure was swallowed: $script:appMode"
if ($script:appMode -eq 'personal-Teams-only') { Assert-OfficeTest ($failure.Exception.Message -match 'Teams setup returned success but the app was not provisioned') 'Legacy personal MicrosoftTeams must not satisfy MSTeams verification.' }
if ($script:appMode -notlike '*NewOutlook') { Assert-OfficeTest (!($script:appCalls -match '^NewOutlook ')) 'Install continued after a companion failure.' }
}
}
@@ -645,12 +665,34 @@ try {
function Get-ItemPropertyValue { param($LiteralPath,$Name,$ErrorAction) if ($LiteralPath -like '*logged-on*') { 'Complete' } }
function Unregister-ScheduledTask { param($TaskName,$Confirm,$ErrorAction) $script:removed += $TaskName }
$profiles = @([pscustomobject]@{SID='logged-on'; Loaded=$true},[pscustomobject]@{SID='logged-off'; Loaded=$false})
$pending = Start-OfficeUserCleanup $profiles ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32))
$cleanup = Start-OfficeUserCleanup $profiles ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32))
Assert-OfficeTest ($script:registered.Count -eq 2 -and $script:started.Count -eq 1 -and $script:removed.Count -eq 1) 'User scheduling or completed task cleanup is incorrect.'
Assert-OfficeTest ($pending.Count -eq 1 -and $pending[0] -eq 'logged-off') 'Logged-off user cleanup was reported complete.'
Assert-OfficeTest ($cleanup.PendingUserSids.Count -eq 1 -and $cleanup.PendingUserSids[0] -eq 'logged-off' -and $cleanup.RegistrationFailures.Count -eq 0) 'Logged-off user cleanup was reported complete.'
function Unregister-ScheduledTask { throw 'Task Scheduler is temporarily unavailable.' }
$pending = Start-OfficeUserCleanup $profiles ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32))
Assert-OfficeTest ($pending.Count -eq 1 -and $pending[0] -eq 'logged-off') 'Completed task housekeeping must not fail deployment or mark completed cleanup pending.'
$cleanup = Start-OfficeUserCleanup $profiles ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32))
Assert-OfficeTest ($cleanup.PendingUserSids.Count -eq 1 -and $cleanup.PendingUserSids[0] -eq 'logged-off') 'Completed task housekeeping must not fail deployment or mark completed cleanup pending.'
function New-ScheduledTaskPrincipal {
param($UserId,$LogonType,$RunLevel,$ErrorAction)
if ($UserId -eq 'unmapped' -and $script:registrationStage -eq 'principal') { throw 'No mapping between account names and security IDs was done.' }
$UserId
}
function Register-ScheduledTask {
param($TaskName,$Action,$Principal,$Trigger,$Settings,[switch]$Force,$ErrorAction)
if ($Principal -eq 'unmapped') { throw 'No mapping between account names and security IDs was done.' }
$script:registered += $TaskName
}
$unmapped = [pscustomobject]@{SID='unmapped'; LocalPath='C:\Users\defaultuser100000'; Loaded=$true}
foreach ($script:registrationStage in @('principal','register')) {
$script:registered=@(); $script:started=@()
$cleanup = @(Start-OfficeUserCleanup (@($unmapped) + $profiles) ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32)) 2>$null)
Assert-OfficeTest ($cleanup.Count -eq 1 -and $cleanup[0].RegistrationFailures.Count -eq 1) 'An unmapped account must return one structured result with a registration failure.'
Assert-OfficeTest ($cleanup[0].RegistrationFailures[0].SID -eq 'unmapped' -and $cleanup[0].RegistrationFailures[0].ProfilePath -eq $unmapped.LocalPath -and $cleanup[0].RegistrationFailures[0].Error -match 'No mapping') 'Registration failures must identify the affected SID, profile, and actual error.'
Assert-OfficeTest ($script:registered.Count -eq 2 -and $script:started.Count -eq 1) 'An unmapped first profile must not prevent scheduling and starting cleanup for later users.'
Assert-OfficeTest ($cleanup[0].PendingUserSids.Count -eq 1 -and $cleanup[0].PendingUserSids[0] -eq 'logged-off') 'A missing task must not be reported as able to retry at sign-in.'
}
$cleanup = Start-OfficeUserCleanup @() ([pscustomobject]@{Helper='C:\secure\helper.ps1'}) (Join-Path $testRoot ('a'*32))
Assert-OfficeTest ($cleanup.PendingUserSids.Count -eq 0 -and $cleanup.RegistrationFailures.Count -eq 0) 'An empty profile list must return empty cleanup result arrays.'
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.'
}
# Real temporary files for cache reuse/tampering; no installer or network may run.
@@ -777,7 +819,13 @@ try {
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 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' }; if ($script:mode -eq 'pending') { return ,@('S-1-5-21-test') }; return ,@() }
function Start-OfficeUserCleanup {
$script:events += 'user'; if ($script:mode -eq 'user') { throw 'user still running' }
$pending = @(); $failures = @()
if ($script:mode -in @('pending','registration-and-pending')) { $pending = @('S-1-5-21-test') }
if ($script:mode -in @('registration','registration-and-pending','registration-reboot')) { $failures = @([pscustomobject]@{SID='unmapped'; ProfilePath='C:\Users\defaultuser100000'; Error='No mapping'}) }
[pscustomobject]@{PendingUserSids=$pending; RegistrationFailures=$failures}
}
function Set-OfficeReinstallPhase { $script:events += 'phase'; if ($script:mode -eq 'phase') { throw 'user still holds cleanup lock' } }
function Install-OfficeMedia { $script:events += 'install'; if ($script:mode -eq 'install') { throw 'install failure' }; return 0 }
function Install-OfficeCompanionApps { param($Media,$Workspace,[bool]$InstallTeams) $script:teamsRequested=$InstallTeams; $script:events += 'install-companions'; if ($script:mode -eq 'install-companions') { throw 'companion install failure' }; [pscustomobject]@{ExitCode=$(if ($script:mode -eq 'companion-reboot') {3010} else {0}); Versions=@{NewOutlook='1'; OneDrive='1'}} }
@@ -802,6 +850,15 @@ try {
$script:mode='cleanup-warning'; $script:events=@()
$result = @(Invoke-OfficeDeployment $options)
Assert-OfficeTest ($result.Count -eq 1 -and $result[0].ArtifactCleanupPending -and $result[0].ExitCode -eq 0 -and $result[0].OfficeDeploymentStatus -eq 'Installed') 'Artifact cleanup problems corrupted the receipt or failed a verified installation.'
foreach ($script:mode in @('registration','registration-and-pending','registration-reboot')) {
$script:events=@(); $script:artifactCleanupRuns=0
function Install-OfficeMedia { $script:events += 'install'; if ($script:mode -eq 'registration-reboot') { return 3010 }; return 0 }
$result = Invoke-OfficeDeployment $options
Assert-OfficeTest ($result.OfficeDeploymentStatus -eq 'InstalledWithUserCleanupIncomplete' -and $result.UserCleanupRegistrationFailures.Count -eq 1 -and $result.UserCleanupRegistrationFailures[0].SID -eq 'unmapped') 'A task registration failure must remain visible after successful installation.'
Assert-OfficeTest ($result.ExitCode -eq $(if ($script:mode -eq 'registration-reboot') {3010} else {2})) 'Registration failures must report partial completion without hiding a required reboot.'
Assert-OfficeTest ($result.PendingUserSids.Count -eq $(if ($script:mode -eq 'registration-and-pending') {1} else {0})) 'Unregistered tasks must remain distinct from deferred logon tasks.'
Assert-OfficeTest ('install' -in $script:events -and 'install-companions' -in $script:events -and $script:artifactCleanupRuns -eq 1) 'Task registration failure must allow verified installation and staging-file cleanup.'
}
foreach ($script:mode in @('reboot','office-reboot')) {
foreach ($script:removalStillRegistered in @($false,$true)) {
$script:events=@()