added a few fixes

This commit is contained in:
2026-10-21 09:30:15 -04:00
parent 11bc57e665
commit 136c88fedf
6 changed files with 1103 additions and 158 deletions
+60 -7
View File
@@ -1,8 +1,13 @@
#Requires -Version 5.1
# EmberkomOfficeUserCleanup-v2
# EmberkomOfficeUserCleanup-v4
# Run only as the affected user, at limited privilege; staged by the Office installer.
param([Parameter(Mandatory = $true)][ValidatePattern('^[a-f0-9]{32}$')][string]$DeploymentId)
function Get-OfficeUserCleanupDefinitions {
# This helper runs independently of Tools.ps1 and the main installer's functions.
@{ MsiBusyAttempts=5; MsiBusyRetrySeconds=15; RegistrationCheckAttempts=13; RegistrationCheckSeconds=5 }
}
function Test-OfficeCredentialTarget {
param([string]$Target)
# Preserve generic Azure AD, WindowsLive, OneDrive and MicrosoftAccount credentials.
@@ -72,21 +77,69 @@ function Remove-OfficeUserLegacyTeams {
if ((Test-Path -LiteralPath (Join-Path $teamsRoot 'current\Teams.exe')) -or (Test-Path -LiteralPath $teamsKey)) { throw 'Classic Teams remains registered or installed for this user.' }
}
function Test-OfficeUserMsiProduct {
param([Parameter(Mandatory=$true)][ValidatePattern('^\{[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\}$')][string]$ProductCode)
if (!('Emberkom.OfficeUserMsi' -as [type])) {
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
namespace Emberkom {
public static class OfficeUserMsi {
[DllImport("msi.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
private static extern uint MsiEnumProductsExW(string product, string userSid, uint context, uint index, IntPtr code, IntPtr installedContext, IntPtr sid, IntPtr sidLength);
public static uint Find(string product) {
// Native NULL selects this user; 3 includes managed/unmanaged per-user products only.
return MsiEnumProductsExW(product, null, 3, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
}
}
}
'@
}
$code = [Emberkom.OfficeUserMsi]::Find($ProductCode)
if ($code -eq 0) { return $true }
if ($code -in @(259, 1605)) { return $false }
throw "Cannot determine this user's MSI registration for $ProductCode (Win32 $code)."
}
function Get-OfficeUserTeamsAddinEntries {
# A per-user MSI can have an HKLM uninstall entry. Ask Windows Installer which
# products belong to this user instead of treating the hive as installation scope.
$roots = @('HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall', 'HKCU:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
$seen = @{}
foreach ($root in $roots) {
if (!(Test-Path -LiteralPath $root)) { continue }
foreach ($entry in @(Get-ItemProperty -Path "$root\*" -ErrorAction Stop | Where-Object { $_.DisplayName -eq 'Microsoft Teams Meeting Add-in for Microsoft Office' -and $_.Publisher -like 'Microsoft*' })) {
if ($seen.ContainsKey($entry.PSChildName)) { continue }
$seen[$entry.PSChildName] = $true
if (Test-OfficeUserMsiProduct $entry.PSChildName) { $entry }
}
}
}
function Remove-OfficeUserCompanions {
param([switch]$PreserveReinstalledApps)
$definitions = Get-OfficeUserCleanupDefinitions
Remove-OfficeUserLegacyTeams
if ($PreserveReinstalledApps) {
Write-Host 'Reinstallation has started. Preserving the Teams meeting add-in and leaving per-user OneDrive migration to the per-machine OneDrive installer.'
return
}
$uninstallRoot = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
if (Test-Path -LiteralPath $uninstallRoot) {
foreach ($entry in @(Get-ItemProperty -Path "$uninstallRoot\*" -ErrorAction Stop | Where-Object { $_.DisplayName -eq 'Microsoft Teams Meeting Add-in for Microsoft Office' -and $_.Publisher -like 'Microsoft*' })) {
if ($entry.PSChildName -notmatch '^\{[0-9A-Fa-f-]{36}\}$') { throw 'Unexpected per-user Teams add-in product code.' }
foreach ($entry in @(Get-OfficeUserTeamsAddinEntries)) {
Write-Host "Removing this user's Teams meeting add-in $($entry.DisplayVersion), $($entry.PSChildName)."
for ($attempt = 1; ; $attempt++) {
$process = Start-Process -FilePath "$env:WINDIR\System32\msiexec.exe" -ArgumentList "/x $($entry.PSChildName) /qn /norestart" -WindowStyle Hidden -Wait -PassThru -ErrorAction Stop
if ($process.ExitCode -notin @(0, 1605, 1614)) { throw "Per-user Teams add-in removal needs attention (exit $($process.ExitCode))." }
if (Test-Path -LiteralPath $entry.PSPath) { throw 'Per-user Teams meeting add-in remains registered.' }
if ($process.ExitCode -ne 1618 -or $attempt -ge $definitions.MsiBusyAttempts) { break }
Write-Host "Windows Installer is busy; retrying the user add-in removal in $($definitions.MsiBusyRetrySeconds) seconds (attempt $attempt of $($definitions.MsiBusyAttempts))."
Start-Sleep -Seconds $definitions.MsiBusyRetrySeconds
}
if ($process.ExitCode -notin @(0, 1605, 1614)) { throw "Per-user Teams add-in removal needs attention (exit $($process.ExitCode)), product $($entry.PSChildName)." }
for ($attempt = 1; $attempt -le $definitions.RegistrationCheckAttempts; $attempt++) {
$remains = Test-OfficeUserMsiProduct $entry.PSChildName
if (!$remains -or $attempt -eq $definitions.RegistrationCheckAttempts) { break }
if ($attempt -eq 1) { Write-Host 'Waiting for the user add-in registration to disappear.' }
Start-Sleep -Seconds $definitions.RegistrationCheckSeconds
}
if ($remains) { throw "Teams meeting add-in $($entry.PSChildName) remains registered for this user." }
}
$oneDriveKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\OneDriveSetup.exe'
if (Test-Path -LiteralPath $oneDriveKey) {