\$ErrorActionPreference = 'Stop' \(KmsHost = 'kms.wicloudplatform.com'\)InstallPath = 'C:\ProgramData\WindowsKMS.ps1' Dism = "env:SystemRoot\System32\dism.exe" Slmgr = "env:SystemRoot\System32\slmgr.vbs" PowerShellExe = "env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe" \$Principal = [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not \$Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Vui lòng chạy PowerShell dưới quyền Administrator.' } if (-not \(PSCommandPath) {\)DownloadedScript = Invoke-WebRequest -Uri 'https://kms.wicloudplatform.com/install.ps1' -UseBasicParsing Set-Content -LiteralPath InstallPath -Value DownloadedScript.Content -Encoding UTF8 & PowerShellExe -NoProfile -ExecutionPolicy Bypass -File InstallPath exit \$LASTEXITCODE } if (PSCommandPath -ne InstallPath) { Copy-Item -LiteralPath PSCommandPath -Destination InstallPath -Force } Set-TimeZone -Name 'SE Asia Standard Time' \$OS = Get-CimInstance Win32_OperatingSystem Caption = OS.Caption \(Build = [int]\)OS.BuildNumber IsServer = Caption -match 'Windows Server' \(CurrentOutput = &\)Dism /Online /Get-CurrentEdition 2>&1 if (\$LASTEXITCODE -ne 0) { throw "Không thể kiểm tra Windows edition: (CurrentOutput -join "`n")" } CurrentText = CurrentOutput -join "`n" $EditionMatch = [regex]::Match($CurrentText, '(?im)^\s*Current Edition\s*:\s*(\S+)\s*$') if (-not $EditionMatch.Success) { throw "Không thể parse thông tin Windows edition." } $Edition = $EditionMatch.Groups[1].Value.Trim() $Year = $null if ($Caption -match 'Windows Server 2025') { $Year = '2025' } elseif ($Caption -match 'Windows Server 2022') { $Year = '2022' } elseif ($Caption -match 'Windows Server 2019') { $Year = '2019' } elseif ($Caption -match 'Windows Server 2016') { $Year = '2016' } elseif ($Caption -match 'Windows Server 2012 R2') { $Year = '2012R2' } $Keys = @{ 'Windows8.1Professional' = 'GCRJD-8NW9H-F2CDX-CCM8D-9D6T9' 'Windows8.1Enterprise' = 'MHF9N-XY6XB-WVXMC-BTDCT-MKKG7' 'Professional' = 'W269N-WFGWX-YVC9B-4J6C9-T83GX' 'ProfessionalWorkstation' = 'NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J' 'Education' = 'NW6C2-QMPVW-D7KKK-3GKT6-VCFB2' 'Enterprise' = 'NPPR9-FWDCX-D2C8J-H872K-2YT43' 'Server2012R2Standard' = 'D2N9P-3P6X9-2R39C-7RTCD-MDVJX' 'Server2012R2Datacenter' = 'W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9' 'Server2016Standard' = 'WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY' 'Server2016Datacenter' = 'CB7KF-BWN84-R7R2Y-793K2-8XDDG' 'Server2019Standard' = 'N69G4-B89J2-4G8F4-WWYCC-J464C' 'Server2019Datacenter' = 'WMDGN-G9PQG-XVVXX-R3X43-63DFG' 'Server2022Standard' = 'VDYBN-27WPP-V4HQT-9VMD4-VMK7H' 'Server2022Datacenter' = 'WX4NM-KYWYW-QJJR4-XV3QB-6VM33' 'Server2025Standard' = 'TVRH6-WHNXV-R9WG3-9XRFY-MY832' 'Server2025Datacenter' = 'D764K-2NDRG-47T6Q-P8T8W-YP6DF' } $KeyName = $null $TargetEdition = $null if ($IsServer) { if (-not $Year) { throw "Phiên bản Windows Server không hỗ trợ: $Caption" } switch ($Edition) { 'ServerStandardEval' { $TargetEdition = 'ServerStandard' $KeyName = "Server${Year}Standard" } 'ServerDatacenterEval' { $TargetEdition = 'ServerDatacenter' $KeyName = "Server${Year}Datacenter" } 'ServerStandard' { $KeyName = "Server${Year}Standard" } 'ServerDatacenter' { $KeyName = "Server${Year}Datacenter" } default { throw "Phiên bản Edition không được hỗ trợ tự động: $Edition" } } } else { switch -Regex ($Caption) { 'Pro$' { $KeyName = 'Professional'; break } 'Education$' { $KeyName = 'Education'; break } 'Enterprise$' { $KeyName = 'Enterprise'; break } default { throw "Phiên bản Windows Client không hỗ trợ: $Caption" } } } if (-not $Keys.ContainsKey($KeyName)) { throw "Không tìm thấy GVLK tương ứng cho $KeyName trong danh sách." } $Key = $Keys[$KeyName] Write-Output "Hệ điều hành: $Caption" Write-Output "Edition hiện tại: $Edition" if ($Edition -match 'Eval') { $TargetOutput = & $Dism /Online /Get-TargetEditions 2>&1 if ($LASTEXITCODE -ne 0) { throw "Không thể lấy danh sách Target Editions." } $Targets = [regex]::Matches(($TargetOutput -join "`n"), '(?im)^\s*Target Edition\s*:\s*\((\S+)\\)s*') | ForEach-Object { \$_.Groups[1].Value.Trim() } if (Targets -notcontains TargetEdition) { throw "Phiên bản \$TargetEdition không nằm trong danh sách chuyển đổi cho phép của OS này." } \(ConvertOutput = &\)Dism /Online "/Set-Edition:TargetEdition" "/ProductKey:Key" /AcceptEula 2>&1 ConvertExitCode = LASTEXITCODE ConvertOutput | ForEach-Object Write-Output _ } if (\$ConvertExitCode -ne 0) { throw "Chuyển đổi Evaluation thất bại với Exit Code \$ConvertExitCode." } Write-Output "Chuyển đổi thành công! Máy tính sẽ khởi động lại sau 5 giây..." Start-Sleep -Seconds 5 Restart-Computer -Force exit } \$InstallOutput = & cscript.exe //Nologo Slmgr /ipk Key 2>&1 if ((\$InstallOutput -join "`n") -match '(?i)error|failed') { throw "Lỗi nạp mã sản phẩm (IPK): $($InstallOutput -join "`n")" } Write-Output "Nạp Key KMS thành công: \$Key" \$KmsOutput = & cscript.exe //Nologo Slmgr /skms KmsHost 2>&1 if ((\$KmsOutput -join "`n") -match '(?i)error|failed') { throw "Lỗi kết nối KMS Server: $($KmsOutput -join "`n")" } Write-Output "Đã cấu hình KMS Server: \$KmsHost" \(ActivationOutput = & cscript.exe //Nologo\)Slmgr /ato 2>&1 if ((\$ActivationOutput -join "`n") -match '(?i)error|failed') { throw "Kích hoạt thất bại: $($ActivationOutput -join "`n")" } Write-Output "Kích hoạt Windows thành công!" & cscript.exe //Nologo \$Slmgr /xpr