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
+27 -12
View File
@@ -53,7 +53,9 @@ function Get-OfficeDeploymentDefinitions {
MinimumServerRevision = 2402
Windows11Build = 22000
TeamsUnsupportedWindows10Editions = @('EnterpriseS', 'EnterpriseSN', 'IoTEnterpriseS')
RunningAppNames = @('lync', 'winword', 'excel', 'msaccess', 'mstore', 'infopath', 'setlang', 'msouc', 'ois', 'onenote', 'outlook', 'powerpnt', 'mspub', 'groove', 'visio', 'winproj', 'graph', 'teams', 'ms-teams', 'OneDrive', 'olk')
# Teams.exe is classic, msteams.exe is the older personal/Windows Chat client,
# and ms-teams.exe is the current client. Check all three before removal.
RunningAppNames = @('lync', 'winword', 'excel', 'msaccess', 'mstore', 'infopath', 'setlang', 'msouc', 'ois', 'onenote', 'outlook', 'powerpnt', 'mspub', 'groove', 'visio', 'winproj', 'graph', 'teams', 'msteams', 'ms-teams', 'OneDrive', 'olk')
AppxRemovalNames = @('Microsoft.Office.Desktop', 'Microsoft.MicrosoftOfficeHub', 'Microsoft.Office.OneNote', 'Microsoft.OutlookForWindows', 'MSTeams', 'MicrosoftTeams')
TeamsMsiNames = @('Teams Machine-Wide Installer', 'Microsoft Teams Meeting Add-in for Microsoft Office')
OfficeIdentityVersions = @('15.0', '16.0')
@@ -623,6 +625,7 @@ function Install-OfficeCompanionApps {
$packages = @(Wait-OfficeResult { Get-AppxProvisionedPackage -Online -ErrorAction Stop | Where-Object { $_.DisplayName -eq $app.Package -and $_.PackageName -like ('*' + $definitions.AppxPublisherSuffix) } } "$($app.Name) provisioning")
if (!$packages.Count) { throw "$($app.Name) setup returned success but the app was not provisioned for all users." }
$versions[$app.Name] = ($packages.Version | Sort-Object -Unique) -join ', '
$null = Write-Output "Verified $($app.Name) provisioned package: $($packages.PackageName -join ', ')."
} else {
$oneDrive = Wait-OfficeResult { Get-OfficeMachineOneDrive } 'per-machine OneDrive registration'
if (!$oneDrive) { throw 'OneDrive setup returned success but a per-machine installation was not detected.' }
@@ -885,15 +888,24 @@ function Clear-OfficeProfileIdentity {
function Start-OfficeUserCleanup {
param([object[]]$Profiles, $Media, [string]$Workspace)
$definitions = Get-OfficeDeploymentDefinitions
$deploymentId = Split-Path -Leaf $Workspace; $tasks = @()
$deploymentId = Split-Path -Leaf $Workspace; $tasks = @(); $registrationFailures = @()
foreach ($profile in $Profiles) {
$name = "Emberkom-OfficeCleanup-$deploymentId-$($profile.SID)"
$action = New-ScheduledTaskAction -Execute "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$($Media.Helper)`" -DeploymentId $deploymentId"
$principal = New-ScheduledTaskPrincipal -UserId $profile.SID -LogonType Interactive -RunLevel Limited
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $profile.SID
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes $definitions.UserCleanupTaskMinutes) -MultipleInstances IgnoreNew
$null = Write-Output "Registering user cleanup task: $name"
$null = Register-ScheduledTask -TaskName $name -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Force -ErrorAction Stop
try {
$action = New-ScheduledTaskAction -Execute "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$($Media.Helper)`" -DeploymentId $deploymentId" -ErrorAction Stop
$principal = New-ScheduledTaskPrincipal -UserId $profile.SID -LogonType Interactive -RunLevel Limited -ErrorAction Stop
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $profile.SID -ErrorAction Stop
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes $definitions.UserCleanupTaskMinutes) -MultipleInstances IgnoreNew -ErrorAction Stop
$null = Write-Output "Registering user cleanup task: $name"
$null = Register-ScheduledTask -TaskName $name -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Force -ErrorAction Stop
} catch {
# A profile can outlive its account, or account lookup can be unavailable.
# Do not leave Office uninstalled because this user's task cannot be created.
# Keep it separate from pending tasks: signing in cannot retry a missing task.
$registrationFailures += [pscustomobject]@{ SID=$profile.SID; ProfilePath=$profile.LocalPath; Error=$_.Exception.Message }
Write-Error "User cleanup task registration failed for $($profile.SID) ($($profile.LocalPath)): $($_.Exception.Message). Continuing Office installation and cleanup scheduling for other users; this user's task needs review." -ErrorAction Continue
continue
}
$tasks += [pscustomobject]@{ Name = $name; SID = $profile.SID; WasLoaded = $profile.Loaded }
if ($profile.Loaded) {
try { Start-ScheduledTask -TaskName $name -ErrorAction Stop }
@@ -915,7 +927,7 @@ function Start-OfficeUserCleanup {
}
else { $pending += $task.SID; $null = Write-Output "Cleanup pending for $($task.SID); task $($task.Name) runs at their next sign-in." }
}
return ,$pending
[pscustomobject]@{ PendingUserSids=$pending; RegistrationFailures=$registrationFailures }
}
function Install-OfficeMedia {
@@ -994,12 +1006,14 @@ function Invoke-OfficeDeployment {
foreach ($profile in $profiles) { Clear-OfficeProfileIdentity $profile }
$userWorkspace = $workspace
if ($profiles.Count) { $userWorkspace = New-OfficeUserWorkspace $media $workspace }
$pending = Start-OfficeUserCleanup $profiles $media $userWorkspace
$userCleanup = Start-OfficeUserCleanup $profiles $media $userWorkspace
$pending = @($userCleanup.PendingUserSids)
$registrationFailures = @($userCleanup.RegistrationFailures)
Set-OfficeReinstallPhase $userWorkspace
$code = Install-OfficeMedia $Options $media $workspace
$companions = Install-OfficeCompanionApps $media.Companions $workspace -InstallTeams $Options.InstallTeams
if ($reboot -or $removalCode -eq 3010 -or $companions.ExitCode -eq 3010) { $code = 3010 }
$status = if ($pending.Count) { 'InstalledWithUserCleanupPending' } else { 'Installed' }
$status = if ($registrationFailures.Count) { 'InstalledWithUserCleanupIncomplete' } elseif ($pending.Count) { 'InstalledWithUserCleanupPending' } else { 'Installed' }
$null = Write-Output "$status. Office $($media.Version), $($Options.ProductId). No automatic reboot was requested."
$provisionedApps = if ($Options.InstallTeams) { 'Teams and new Outlook are' } else { 'New Outlook is' }
$null = Write-Output "$provisionedApps provisioned for all users; OneDrive is installed per-machine. Users may need to sign out and back in to register the apps."
@@ -1010,7 +1024,7 @@ function Invoke-OfficeDeployment {
}
catch { $cleanupWarnings = @($_.Exception.Message); Write-Error "Office deployment file cleanup was incomplete: $($cleanupWarnings[0])" -ErrorAction Continue }
if ($cleanupWarnings.Count) { $null = Write-Output 'Installation succeeded, but some deployment files remain; see the diagnostic log.' }
[pscustomobject]@{ OfficeDeploymentStatus = $status; ExitCode = $(if ($pending.Count -and $code -eq 0) { 2 } else { $code }); RestartRequired=($code -eq 3010); DeferReboot=$Options.DeferReboot; ValidateBeforeChanges=$false; Workspace = $workspace; UserCleanupWorkspace=$(if ($profiles.Count) { $userWorkspace } else { $null }); PendingUserSids = $pending; CompanionApps=$companions.Versions; TeamsSelected=$Options.InstallTeams; ProductId=$Options.ProductId; OfficeVersion=$media.Version; CachedMediaReused=[bool]$media.CachedMediaReused; ArtifactCleanupPending=($cleanupWarnings.Count -gt 0) }
[pscustomobject]@{ OfficeDeploymentStatus = $status; ExitCode = $(if (($pending.Count -or $registrationFailures.Count) -and $code -eq 0) { 2 } else { $code }); RestartRequired=($code -eq 3010); DeferReboot=$Options.DeferReboot; ValidateBeforeChanges=$false; Workspace = $workspace; UserCleanupWorkspace=$(if ($profiles.Count) { $userWorkspace } else { $null }); PendingUserSids = $pending; UserCleanupRegistrationFailures = $registrationFailures; CompanionApps=$companions.Versions; TeamsSelected=$Options.InstallTeams; ProductId=$Options.ProductId; OfficeVersion=$media.Version; CachedMediaReused=[bool]$media.CachedMediaReused; ArtifactCleanupPending=($cleanupWarnings.Count -gt 0) }
} finally {
if ($locked) { $mutex.ReleaseMutex() }
$mutex.Dispose()
@@ -1077,6 +1091,7 @@ function Invoke-OfficeDeploymentEntry {
$null = Write-Output $receipt
Microsoft.PowerShell.Utility\Write-Output $receipt
if ($deployment[0].OfficeDeploymentStatus -like 'Installed*') {
if ($deployment[0].UserCleanupRegistrationFailures.Count) { Microsoft.PowerShell.Utility\Write-Output 'WARNING: Installation succeeded, but some user cleanup tasks could not be registered. Review UserCleanupRegistrationFailures; signing in alone cannot retry a task that was not created.' }
if ($deployment[0].ArtifactCleanupPending) { Microsoft.PowerShell.Utility\Write-Output 'WARNING: Installation succeeded, but some deployment files could not be cleaned up. See the diagnostic log; no background cleanup is scheduled.' }
if ($deployment[0].ExitCode -eq 3010) { Microsoft.PowerShell.Utility\Write-Output 'A restart is required to finish deployment; no automatic reboot was requested.' }
Microsoft.PowerShell.Utility\Write-Output 'Users may need to sign out and back in for companion app registration; check PendingUserSids for deferred cleanup.'