kms-website: make activation scripts idempotent + harden Office detection

- Replace locale-dependent "License Status: Licensed" regex with a
  locale-independent WMI probe (SoftwareLicensingProduct.LicenseStatus==1).
  Fixes false "not licensed yet" reports on non-English Windows and on re-runs.
- Idempotent: always pin the KMS host, but skip /ato (Windows) and /act
  (Office) when already licensed — report days remaining instead of
  re-contacting the public KMS server.
- Find-Ospp now also checks Click-to-Run \root\Office16\ (+ \root\Office15\)
  layouts, not just the MSI Office16 path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-06-01 08:53:20 +00:00
parent efa1353e6b
commit d11dc8c0ce
2 changed files with 66 additions and 31 deletions

View file

@ -72,27 +72,40 @@ if (-not ($doWin -or $doOfficeAct -or $doProjAct -or $doVisioAct)) {
}
# --- Windows -------------------------------------------------------------
# Locale-independent license probe (slmgr /dlv text is localized; the WMI
# LicenseStatus integer is not). 1 = Licensed. $null = no KMS-client SKU.
function Get-WindowsLicense {
$q = "SELECT LicenseStatus, GracePeriodRemaining FROM SoftwareLicensingProduct WHERE Name LIKE 'Windows%' AND PartialProductKey IS NOT NULL"
$p = $null
try { $p = Get-CimInstance -Query $q -ErrorAction Stop | Select-Object -First 1 }
catch { try { $p = Get-WmiObject -Query $q -ErrorAction Stop | Select-Object -First 1 } catch {} }
if (-not $p) { return $null }
[pscustomobject]@{ Licensed = ($p.LicenseStatus -eq 1); DaysLeft = [int]([math]::Round($p.GracePeriodRemaining / 1440)) }
}
function Activate-Windows {
Step "Windows activation"
$slmgr = "$env:WINDIR\System32\slmgr.vbs"
& cscript //Nologo $slmgr /skms "$KmsHost`:$KmsPort" | Out-Host
if ($LASTEXITCODE -ne 0) { Bad "slmgr /skms failed"; return }
$lic = Get-WindowsLicense
if ($null -eq $lic) { Bad "No Volume License Windows SKU — install a GVLK first (slmgr /ipk <GVLK>)."; return }
if ($lic.Licensed) { OK "Windows already licensed ($($lic.DaysLeft) days) — host pinned, skipping /ato"; return }
& cscript //Nologo $slmgr /ato | Out-Host
if ($LASTEXITCODE -ne 0) {
Bad "slmgr /ato failed. Most likely cause: not a Volume License edition (Home/retail/OEM cannot KMS-activate)."
Write-Host " See https://kms.viktorbarzin.me/#faq"
return
}
$dlv = & cscript //Nologo $slmgr /dlv 2>&1 | Out-String
if ($dlv -match 'License Status:\s*Licensed') { OK "Windows licensed" } else { Warn "Status not 'Licensed' yet — try 'slmgr /dlv' in a minute" }
$lic = Get-WindowsLicense
if ($lic.Licensed) { OK "Windows licensed ($($lic.DaysLeft) days)" }
else { Bad "Windows not licensed — likely not a VL edition (Home/retail/OEM reject KMS). See https://kms.viktorbarzin.me/#faq" }
}
if ($doWin) { Activate-Windows }
# --- Office / Project / Visio: activate already-installed ----------------
function Find-Ospp {
# Covers MSI (Office16/15) and Click-to-Run (\root\Office16) layouts, 64- and 32-bit.
$candidates = @(
"${env:ProgramFiles}\Microsoft Office\Office16\ospp.vbs",
"${env:ProgramFiles(x86)}\Microsoft Office\Office16\ospp.vbs",
"${env:ProgramFiles}\Microsoft Office\root\Office16\ospp.vbs",
"${env:ProgramFiles(x86)}\Microsoft Office\root\Office16\ospp.vbs",
"${env:ProgramFiles}\Microsoft Office\Office15\ospp.vbs",
"${env:ProgramFiles(x86)}\Microsoft Office\Office15\ospp.vbs"
)
@ -109,9 +122,13 @@ function Activate-Ospp([string]$label) {
Step "$label activation via $ospp"
& cscript //Nologo $ospp /sethst:$KmsHost | Out-Host
& cscript //Nologo $ospp /setprt:$KmsPort | Out-Host
& cscript //Nologo $ospp /act | Out-Host
# Idempotent: skip /act when already licensed (the '---LICENSED---' marker
# in ospp output is a fixed literal, not localized).
$st = & cscript //Nologo $ospp /dstatus 2>&1 | Out-String
if ($st -match '---LICENSED---') { OK "$label licensed" } else { Warn "$label status not LICENSED yet" }
if ($st -match '---LICENSED---') { OK "$label already licensed — host set, skipping /act"; return }
& cscript //Nologo $ospp /act | Out-Host
$st = & cscript //Nologo $ospp /dstatus 2>&1 | Out-String
if ($st -match '---LICENSED---') { OK "$label licensed" } else { Warn "$label status not LICENSED yet (no VL Office SKU? See https://kms.viktorbarzin.me/#office)" }
}
if ($doOfficeAct) { Activate-Ospp 'Office' }
if ($doProjAct) { Activate-Ospp 'Project' }