remove version pinning except for latest supported version for Windows 10

removed checksums, rely only on direct MS download completeness
This commit is contained in:
2026-10-22 13:53:58 -04:00
parent 031cfe40a1
commit 0030be0c7b
3 changed files with 171 additions and 23 deletions
+68 -13
View File
@@ -52,6 +52,15 @@ function Get-OfficeDeploymentDefinitions {
MinimumServerBuild = 20348
MinimumServerRevision = 2402
Windows11Build = 22000
# Windows 10 stays on this feature release; resolve its latest published build
# per channel at run time rather than maintaining a fixed patch/build number.
Windows10OfficeRelease = '2608'
OfficeReleaseHistory = 'https://learn.microsoft.com/en-us/officeupdates/update-history-microsoft365-apps-by-date'
OfficeReleaseHistoryChannels = @{
Current = 'current-channel'
MonthlyEnterprise = 'monthly-enterprise-channel'
SemiAnnual = 'semi-annual-enterprise-channel'
}
TeamsUnsupportedWindows10Editions = @('EnterpriseS', 'EnterpriseSN', 'IoTEnterpriseS')
# 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.
@@ -103,6 +112,8 @@ function Get-OfficeDeploymentOptions {
$company = $company.Trim()
if ($company -eq 'None') { $company = '' }
$options = @{ OfficeSubscription = $subscription; ProductId = $plans[$subscription]; Company = $company; ValidateBeforeChanges = $validateOnly; DeferReboot = $deferReboot }
# Internal values, populated from the host and Microsoft's release history, not RMM input.
$options.OfficeFeatureRelease = ''; $options.TargetOfficeVersion = ''
# Derive this from the validated product mapping, including standalone Apps plans and legacy aliases.
$options.InstallTeams = $options.ProductId -in $definitions.TeamsProducts
foreach ($name in $choices.Keys) {
@@ -131,6 +142,33 @@ function Assert-OfficeDeploymentHost {
$os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop
$windows = Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -ErrorAction Stop
Assert-OfficeCompanionPlatform -Build ([int]$os.BuildNumber) -ProductType ([int]$os.ProductType) -Revision ([int]$windows.UBR) -EditionId $windows.EditionID -InstallTeams $Options.InstallTeams
$Options.OfficeFeatureRelease = Get-OfficeFeatureRelease -Build ([int]$os.BuildNumber) -ProductType ([int]$os.ProductType)
$null = Write-Output "Deployment host: $($os.Caption), build $($os.BuildNumber).$($windows.UBR)."
}
function Get-OfficeFeatureRelease {
param([int]$Build, [int]$ProductType)
$definitions = Get-OfficeDeploymentDefinitions
if ($ProductType -eq 1 -and $Build -ge $definitions.MinimumWindowsBuild -and $Build -lt $definitions.Windows11Build) { return $definitions.Windows10OfficeRelease }
return ''
}
function Get-OfficeBuildFromHistory {
param([string]$PageContent, [string]$UpdateChannel, [string]$FeatureRelease)
$definitions = Get-OfficeDeploymentDefinitions
$article = $definitions.OfficeReleaseHistoryChannels[$UpdateChannel]
if (!$article -or $FeatureRelease -notmatch '^\d{4}$') { throw 'Invalid Office release-history selection.' }
$linkPattern = '(^|/)' + [regex]::Escape($article) + '#version-' + $FeatureRelease + '(?:-|$)'
$labelPattern = '^\s*Version\s+' + $FeatureRelease + '\s+\(Build\s+(?<build>\d+\.\d+)\)\s*$'
$content = [Net.WebUtility]::HtmlDecode($PageContent)
$builds = @(foreach ($link in [regex]::Matches($content, '<a\b[^>]*\bhref\s*=\s*["''](?<href>[^"'']+)["''][^>]*>(?<label>.*?)</a>', 'IgnoreCase,Singleline')) {
if ($link.Groups['href'].Value -notmatch $linkPattern) { continue }
$label = $link.Groups['label'].Value -replace '<[^>]+>', ''
if ($label -match $labelPattern) { [version]('16.0.' + $Matches['build']) }
})
$latest = $builds | Sort-Object -Descending -Unique | Select-Object -First 1
if (!$latest) { throw "Microsoft's release history did not identify an Office $FeatureRelease build for $UpdateChannel. Existing Office has not been removed." }
$latest.ToString()
}
function Assert-OfficeCompanionPlatform {
@@ -280,14 +318,13 @@ function Get-OfficeMediaCacheKey {
$definitions = Get-OfficeDeploymentDefinitions
# Company, activation, app shutdown, and reboot choices affect XML, not the payload.
$contract = [ordered]@{
Schema=1; Product=$Options.ProductId; Architecture=$Options.OfficeArchitecture
Schema=2; Product=$Options.ProductId; Architecture=$Options.OfficeArchitecture
Channel=$Options.UpdateChannel; Language=$definitions.Language
TargetVersion=$Options.TargetOfficeVersion
OdtPage=$definitions.OdtPage; ExcludedApps=$definitions.OdtExcludedApps
Downloads=@(Get-OfficeCompanionDownloads -InstallTeams $Options.InstallTeams | ForEach-Object { "$($_.Name)|$($_.File)|$($_.Uri)" })
}
$sha = [Security.Cryptography.SHA256]::Create()
try { ([BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes(($contract | ConvertTo-Json -Depth 5 -Compress))))).Replace('-', '') }
finally { $sha.Dispose() }
$contract | ConvertTo-Json -Depth 5 -Compress
}
function Save-OfficeMediaCache {
@@ -296,10 +333,10 @@ function Save-OfficeMediaCache {
$files = @()
foreach ($name in @('ODT', 'odt.exe') + @(Get-OfficeCompanionDownloads -InstallTeams $Options.InstallTeams | ForEach-Object { $_.File })) {
foreach ($file in @(Get-OfficeManagedFiles $root (Join-Path $Workspace $name))) {
$files += [pscustomobject]@{ Path=$file.FullName.Substring($Workspace.TrimEnd('\').Length + 1); Length=$file.Length; Hash=(Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256 -ErrorAction Stop).Hash }
$files += [pscustomobject]@{ Path=$file.FullName.Substring($Workspace.TrimEnd('\').Length + 1); Length=$file.Length }
}
}
[ordered]@{ Schema=1; Key=(Get-OfficeMediaCacheKey $Options); CreatedUtc=$CreatedUtc; Version=$Media.Version; Files=$files } |
[ordered]@{ Schema=2; Key=(Get-OfficeMediaCacheKey $Options); CreatedUtc=$CreatedUtc; Version=$Media.Version; Files=$files } |
ConvertTo-Json -Depth 5 | Set-Content -LiteralPath (Join-Path $Workspace 'media-cache.json') -Encoding UTF8 -ErrorAction Stop
}
@@ -317,17 +354,18 @@ function Restore-OfficeMediaCache {
Assert-OfficeManagedPath $root $manifestPath
$manifest = Get-Content -LiteralPath $manifestPath -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
$created = [DateTime]::Parse($manifest.CreatedUtc).ToUniversalTime()
if ($manifest.Schema -ne 1 -or $manifest.Key -ne $key -or $created -gt [DateTime]::UtcNow -or $created -lt [DateTime]::UtcNow.AddDays(-$definitions.MediaCacheDays)) { continue }
if ($manifest.Schema -ne 2 -or $manifest.Key -ne $key -or $created -gt [DateTime]::UtcNow -or $created -lt [DateTime]::UtcNow.AddDays(-$definitions.MediaCacheDays)) { continue }
if ($manifest.Version -notmatch $definitions.OfficeBuildPattern -or !$manifest.Files.Count) { throw 'Invalid cache manifest.' }
if ($Options.TargetOfficeVersion -and $manifest.Version -ne $Options.TargetOfficeVersion) { throw 'Cached Office build does not match the Windows 10 target.' }
$names = @('ODT', 'odt.exe') + @(Get-OfficeCompanionDownloads -InstallTeams $Options.InstallTeams | ForEach-Object { $_.File })
foreach ($entry in $manifest.Files) {
if ([IO.Path]::IsPathRooted($entry.Path) -or $entry.Path -match '(^|[\\/])\.\.([\\/]|$)' -or ($entry.Path -split '[\\/]')[0] -notin $names) { throw 'Invalid cached file path.' }
$path = Join-Path $candidate.FullName $entry.Path
Assert-OfficeManagedPath $candidate.FullName $path
$file = Get-Item -LiteralPath $path -ErrorAction Stop
if ($file.Length -ne $entry.Length -or (Get-FileHash -LiteralPath $path -Algorithm SHA256 -ErrorAction Stop).Hash -ne $entry.Hash) { throw "Cached file changed: $path" }
if ($file.PSIsContainer -or $file.Length -ne $entry.Length) { throw "Cached file is incomplete: $path" }
}
# Verify every executable we will launch, in addition to the cached hashes.
# Keep publisher checks on executable installers; do not hash the Office payload.
foreach ($name in @('odt.exe', 'ODT\setup.exe') + @(Get-OfficeCompanionDownloads -InstallTeams $Options.InstallTeams | ForEach-Object { $_.File })) {
Assert-OfficeMicrosoftSignature (Join-Path $candidate.FullName $name)
}
@@ -477,6 +515,15 @@ function Initialize-OfficeMedia {
param($Options, [string]$Workspace)
$definitions = Get-OfficeDeploymentDefinitions
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Options.TargetOfficeVersion = ''
if ($Options.OfficeFeatureRelease) {
$historyPath = Join-Path $Workspace 'office-release-history.html'
Save-OfficeDownload $definitions.OfficeReleaseHistory $historyPath
$Options.TargetOfficeVersion = Get-OfficeBuildFromHistory (Get-Content -LiteralPath $historyPath -Raw -ErrorAction Stop) $Options.UpdateChannel $Options.OfficeFeatureRelease
$null = Write-Output "Windows 10: selecting Office $($Options.OfficeFeatureRelease), build $($Options.TargetOfficeVersion), from Microsoft's $($Options.UpdateChannel) release history."
} else {
$null = Write-Output "ODT will select the latest available Office version in $($Options.UpdateChannel); no version is specified."
}
# Always refresh the small helper from this repository, even when using cached media.
$helper = Join-Path $Workspace 'Office-UserCleanup.ps1'
Save-OfficeDownload "$($definitions.RepositoryRoot)/resources/office/Office-UserCleanup.ps1" $helper
@@ -487,7 +534,7 @@ function Initialize-OfficeMedia {
if ($cached) {
$odtFolder = Join-Path $Workspace 'ODT'
$configPath = Join-Path $Workspace 'install.xml'; $removalPath = Join-Path $Workspace 'remove.xml'
$config = New-OfficeConfiguration $Options $odtFolder $cached.Version; $config.Save($configPath)
$config = New-OfficeConfiguration $Options $odtFolder $Options.TargetOfficeVersion; $config.Save($configPath)
$removal = New-OfficeRemovalConfiguration $Options; $removal.Save($removalPath)
$companions = @{}
foreach ($download in (Get-OfficeCompanionDownloads -InstallTeams $Options.InstallTeams)) { $companions[$download.Name] = Join-Path $Workspace $download.File }
@@ -509,7 +556,7 @@ function Initialize-OfficeMedia {
$removalPath = Join-Path $Workspace 'remove.xml'
$removal = New-OfficeRemovalConfiguration $Options; $removal.Save($removalPath)
$configPath = Join-Path $Workspace 'install.xml'
$config = New-OfficeConfiguration $Options $odtFolder; $config.Save($configPath)
$config = New-OfficeConfiguration $Options $odtFolder $Options.TargetOfficeVersion; $config.Save($configPath)
$null = Write-Output 'Downloading Office installation files before removing installed apps.'
for ($attempt = 1; $attempt -le $definitions.DownloadAttempts; $attempt++) {
$prefix = if ($attempt -eq 1) { 'office-download' } else { "office-download.retry-$attempt" }
@@ -521,11 +568,11 @@ function Initialize-OfficeMedia {
if ($code -ne 0) { throw "Office download failed ($code). Existing Office has not been removed." }
$versions = @(Get-ChildItem -LiteralPath (Join-Path $odtFolder 'Office\Data') -Directory -ErrorAction Stop | Where-Object { $_.Name -match $definitions.OfficeBuildPattern })
if ($versions.Count -ne 1) { throw 'Office download did not produce one identifiable build.' }
if ($Options.TargetOfficeVersion -and $versions[0].Name -ne $Options.TargetOfficeVersion) { throw 'Downloaded Office build does not match the Windows 10 target. Existing Office has not been removed.' }
$arch = if ($Options.OfficeArchitecture -eq '64') { 'x64' } else { 'x86' }
foreach ($stream in @("stream.$arch.x-none.dat", "stream.$arch.$($definitions.Language).dat")) {
if ((Get-Item -LiteralPath (Join-Path $versions[0].FullName $stream) -ErrorAction Stop).Length -eq 0) { throw "Office payload is empty: $stream" }
}
$config.Configuration.Add.SetAttribute('Version', $versions[0].Name); $config.Save($configPath)
$companions = Initialize-OfficeCompanionMedia $Workspace -InstallTeams $Options.InstallTeams
$media = [pscustomobject]@{ Setup = $setup; RemovalConfig = $removalPath; Config = $configPath; Helper = $helper; Version = $versions[0].Name; Companions = $companions; CachedMediaReused=$false }
Save-OfficeMediaCache $Options $media $Workspace
@@ -945,9 +992,17 @@ function Install-OfficeMedia {
$key = 'HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration'
if (!(Test-Path -LiteralPath $key -ErrorAction Stop)) { return $false }
$installed = Get-ItemProperty -LiteralPath $key -ErrorAction Stop
$Options.ProductId -in ($installed.ProductReleaseIds -split ',') -and $installed.Platform -eq $platform -and $installed.VersionToReport -eq $Media.Version
if ($Options.ProductId -notin ($installed.ProductReleaseIds -split ',') -or $installed.Platform -ne $platform -or $installed.VersionToReport -notmatch (Get-OfficeDeploymentDefinitions).OfficeBuildPattern) { return $false }
# Unpinned ODT can install a newer build than the earlier validation download.
# Windows 10 must still match its explicitly selected compatible build.
if ($Options.TargetOfficeVersion) {
if ($installed.VersionToReport -ne $Options.TargetOfficeVersion) { return $false }
} elseif ([version]$installed.VersionToReport -lt [version]$Media.Version) { return $false }
$installed.VersionToReport
} 'the selected Office product, architecture, and version'
if (!$verified) { throw 'Setup exited successfully but the installed product, architecture, or version did not match.' }
$Media.Version = [string]$verified
$null = Write-Output "Verified installed Office $($Media.Version) ($($Options.ProductId), $platform)."
# Microsoft requires a restart to finish MSI removal even when setup doesn't enforce it.
if ($msi.Count) { $null = Write-Output 'Legacy MSI Office removal was verified. A restart is required to finish removal; no automatic reboot was requested.'; return 3010 }
$code