133 lines
4.5 KiB
PowerShell
133 lines
4.5 KiB
PowerShell
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
$ErrorActionPreference = 'Continue'
|
|
|
|
Write-Host "[*] Windows 啟用腳本"
|
|
Write-Host ""
|
|
|
|
# 檢查管理員權限
|
|
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
Write-Host "[!] 需要管理員權限才能執行此腳本" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# 檢查目前啟用狀態
|
|
Write-Host "[*] 檢查目前啟用狀態"
|
|
$license = Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey}
|
|
$currentStatus = $license | Select-Object Name, LicenseStatus, Description, GracePeriodRemaining
|
|
|
|
Write-Host "目前狀態:$($currentStatus.Name)"
|
|
Write-Host "授權狀態:$($currentStatus.LicenseStatus) ($($currentStatus.Description))"
|
|
Write-Host "剩餘期間:$($currentStatus.GracePeriodRemaining) 分鐘"
|
|
Write-Host ""
|
|
|
|
if ($currentStatus.LicenseStatus -eq 1) {
|
|
Write-Host "[OK] Windows 已經啟用,無需執行啟用程序" -ForegroundColor Green
|
|
exit 0
|
|
}
|
|
|
|
# 偵測 Windows 版本並設定對應的 GVLK
|
|
Write-Host "[*] 偵測 Windows 版本"
|
|
$osInfo = Get-ComputerInfo
|
|
$windowsEdition = $osInfo.WindowsProductName
|
|
Write-Host "Windows 版本:$windowsEdition"
|
|
|
|
# 常見 Windows 版本的 GVLK (Generic Volume License Keys)
|
|
$gvlkKeys = @{
|
|
'Windows 10 Pro' = 'W269N-WFGWX-YVC9B-4J6C9-T83GX'
|
|
'Windows 10 Professional' = 'W269N-WFGWX-YVC9B-4J6C9-T83GX'
|
|
'Windows 10 Home' = 'TX9XD-98N7V-6WMQ6-BX7FG-H8Q99'
|
|
'Windows 10 Enterprise' = 'NPPR9-FWDCX-D2C8J-H872K-2YT43'
|
|
'Windows 11 Pro' = 'W269N-WFGWX-YVC9B-4J6C9-T83GX'
|
|
'Windows 11 Professional' = 'W269N-WFGWX-YVC9B-4J6C9-T83GX'
|
|
'Windows 11 Home' = 'TX9XD-98N7V-6WMQ6-BX7FG-H8Q99'
|
|
'Windows 11 Enterprise' = 'NPPR9-FWDCX-D2C8J-H872K-2YT43'
|
|
}
|
|
|
|
$productKey = $null
|
|
foreach ($edition in $gvlkKeys.Keys) {
|
|
if ($windowsEdition -like "*$edition*") {
|
|
$productKey = $gvlkKeys[$edition]
|
|
break
|
|
}
|
|
}
|
|
|
|
if (-not $productKey) {
|
|
# 預設使用 Pro 版本的金鑰
|
|
$productKey = 'W269N-WFGWX-YVC9B-4J6C9-T83GX'
|
|
Write-Host "[*] 無法偵測到確切版本,使用 Pro 版本金鑰" -ForegroundColor Yellow
|
|
}
|
|
|
|
Write-Host "使用產品金鑰:$productKey"
|
|
Write-Host ""
|
|
|
|
# KMS 伺服器清單
|
|
$kmsServers = @(
|
|
'kms.digiboy.ir',
|
|
'kms8.msguides.com',
|
|
'kms.03k.org',
|
|
'kms.chinancce.com'
|
|
)
|
|
|
|
Write-Host "[*] 開始啟用程序"
|
|
|
|
# 1. 安裝產品金鑰
|
|
Write-Host "[1/3] 安裝產品金鑰"
|
|
try {
|
|
& slmgr /ipk $productKey
|
|
Start-Sleep -Seconds 3
|
|
Write-Host " 產品金鑰安裝成功"
|
|
} catch {
|
|
Write-Host " [!] 產品金鑰安裝失敗: $($_.Exception.Message)" -ForegroundColor Red
|
|
}
|
|
|
|
# 2. 嘗試 KMS 伺服器
|
|
Write-Host "[2/3] 設定 KMS 伺服器並啟用"
|
|
$activated = $false
|
|
|
|
foreach ($kmsServer in $kmsServers) {
|
|
Write-Host " 嘗試 KMS 伺服器:$kmsServer"
|
|
|
|
try {
|
|
# 設定 KMS 伺服器
|
|
& slmgr /skms $kmsServer
|
|
Start-Sleep -Seconds 2
|
|
|
|
# 執行啟用
|
|
& slmgr /ato
|
|
Start-Sleep -Seconds 5
|
|
|
|
# 檢查啟用結果
|
|
$newStatus = Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey}
|
|
if ($newStatus.LicenseStatus -eq 1) {
|
|
$activated = $true
|
|
Write-Host " [OK] 啟用成功!" -ForegroundColor Green
|
|
break
|
|
} else {
|
|
Write-Host " 啟用未成功,嘗試下一個伺服器"
|
|
}
|
|
} catch {
|
|
Write-Host " KMS 伺服器連線失敗: $($_.Exception.Message)" -ForegroundColor Yellow
|
|
}
|
|
}
|
|
|
|
# 3. 驗證啟用結果
|
|
Write-Host "[3/3] 驗證啟用結果"
|
|
$finalStatus = Get-CimInstance -ClassName SoftwareLicensingProduct | Where-Object {$_.PartialProductKey}
|
|
|
|
Write-Host ""
|
|
Write-Host "=== 啟用結果 ==="
|
|
Write-Host "版本:$($finalStatus.Name)"
|
|
Write-Host "狀態:$($finalStatus.LicenseStatus) ($($finalStatus.Description))"
|
|
Write-Host "剩餘期間:$($finalStatus.GracePeriodRemaining) 分鐘 (約 $([math]::Round($finalStatus.GracePeriodRemaining / 1440, 1)) 天)"
|
|
|
|
if ($finalStatus.LicenseStatus -eq 1) {
|
|
Write-Host ""
|
|
Write-Host "[OK] Windows 啟用成功!" -ForegroundColor Green
|
|
Write-Host "KMS 啟用有效期約 180 天,會自動續期。"
|
|
exit 0
|
|
} else {
|
|
Write-Host ""
|
|
Write-Host "[!] Windows 啟用失敗" -ForegroundColor Red
|
|
Write-Host "請檢查網路連線或嘗試其他啟用方法。"
|
|
exit 1
|
|
} |