. "$PSScriptRoot\_common.ps1" # 內網 KMS 主機請改下面這列;保持空陣列就會用公開 KMS 清單輪流試 $InternalKmsServers = @() $PublicKmsServers = @('kms.digiboy.ir', 'kms8.msguides.com', 'kms.03k.org', 'kms.chinancce.com') function Test-WindowsActivated { # ApplicationID 55c92734-... = Windows # LicenseStatus: 0=Unlicensed, 1=Licensed, 2=OOB grace, 3=OOT grace, 4=NonGenuine grace, 5=Notification, 6=Extended grace $slp = Get-CimInstance -ClassName SoftwareLicensingProduct -ErrorAction SilentlyContinue | Where-Object { $_.PartialProductKey -and $_.ApplicationID -eq '55c92734-d682-4d71-983e-d6ec3f16059f' -and $_.LicenseStatus -eq 1 } return [bool]$slp } Section 'kms-activate' { Wait-Network -timeoutSec 30 if (Test-WindowsActivated) { Log 'Already activated; skipping KMS attempts' return } $servers = @() if ($InternalKmsServers.Count -gt 0) { $servers += $InternalKmsServers } $servers += $PublicKmsServers $activated = $false foreach ($kms in $servers) { Log "Try KMS: $kms" cscript.exe //nologo C:\Windows\System32\slmgr.vbs /skms $kms | Out-Null cscript.exe //nologo C:\Windows\System32\slmgr.vbs /ato 2>&1 | Out-Null Start-Sleep 1 if (Test-WindowsActivated) { Log "Activated via $kms" $activated = $true break } else { Log " not activated yet" } } if (-not $activated) { Log 'All KMS servers failed; remaining in grace period' } }