{"id":627,"date":"2021-10-28T10:24:17","date_gmt":"2021-10-28T09:24:17","guid":{"rendered":"https:\/\/www.server1.dk\/?p=627"},"modified":"2021-10-28T10:24:17","modified_gmt":"2021-10-28T09:24:17","slug":"powershell-script-to-activate-client-kms-on-windows-server","status":"publish","type":"post","link":"https:\/\/www.server1.dk\/?p=627","title":{"rendered":"Powershell script to activate client KMS on Windows Server"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Elevate to adm\nif (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {\n if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {\n  $CommandLine = \"-File `\"\" + $MyInvocation.MyCommand.Path + \"`\" \" + $MyInvocation.UnboundArguments\n  Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine\n  Exit\n }\n}\n\n# Find OS\n$OS = (Get-WMIObject win32_operatingsystem).Caption\n\nif ($OS.Length -lt 2) { Write-host -ForegroundColor Yellow \"Unable to determine OS\"; Break}\nElse { Write-host -ForegroundColor Green \"$OS\"}\n\n# Find domain and check DNS\n$Domain = (Get-WmiObject win32_computersystem).Domain\n$DnsOutPUT = Resolve-DnsName -Name _vlmcs._tcp.$domain -Type SRV -ErrorAction SilentlyContinue\nif ($DnsOutPUT.Length -lt 2) { Write-host -ForegroundColor Yellow \"Unable to determine DNS records for KMS\"; Break}\n\nElse { Write-host -ForegroundColor Green \"KMS server(s): \" $DnsOutPUT.NameTarget}\n\n# Test connectivity to KMS servers\nforeach ( $KMSserver in $DnsOutPUT.NameTarget ) {\nif ((Test-NetConnection -ComputerName $KMSserver -Port 1688 -ErrorAction SilentlyContinue).TcpTestSucceeded) { Write-host -ForegroundColor Green \"Succesfully connected to:\" $KMSserver}\n}\n\n\n# Build Array of KMS keys - https:\/\/docs.microsoft.com\/en-us\/windows-server\/get-started\/kms-client-activation-keys\n$kmsKeys = @()\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2022 Datacenter\";'Key'=\"WX4NM-KYWYW-QJJR4-XV3QB-6VM33\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2022 Standard\";'Key'=\"VDYBN-27WPP-V4HQT-9VMD4-VMK7H\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2019 Datacenter\";'Key'=\"WMDGN-G9PQG-XVVXX-R3X43-63DFG\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2019 Standard\";'Key'=\"N69G4-B89J2-4G8F4-WWYCC-J464C\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2019 Essentials\";'Key'=\"WVDHN-86M7X-466P6-VHXV7-YY726\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2016 Datacenter\";'Key'=\"CB7KF-BWN84-R7R2Y-793K2-8XDDG\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2016 Standard\";'Key'=\"WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2016 Essentials\";'Key'=\"JCKRF-N37P4-C2D82-9YXRT-4M63B\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2012 R2 Standard\";'Key'=\"D2N9P-3P6X9-2R39C-7RTCD-MDVJX\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2012 R2 Datacenter\";'Key'=\"W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2012 R2 Essentials\";'Key'=\"KNC87-3J2TX-XB4WP-VCPJV-M4FWM\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2008 R2 Standard\";'Key'=\"YC6KT-GKW9T-YTKYR-T4X34-R7VHC\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2008 R2 Enterprise\";'Key'=\"489J6-VHDMP-X63PK-3K798-CPX3Y\" })\n $kmsKeys += New-Object \u2013TypeName PSObject \u2013Prop ([ordered]@{'OS'=\"Microsoft Windows Server 2008 R2 Datacenter\";'Key'=\"74YFP-3QFB3-KQT8W-PMXWJ-7M648\" })\n\n# Set KMS Key\n$SetKeyOutPut = cscript c:\\windows\\system32\\slmgr.vbs \/ipk $kmsKeys[$kmsKeys.os.IndexOf($OS)].key\n$SetKeyOutClean = $SetKeyOutPut | select -last 2 | select -First 1\n\nif ($SetKeyOutClean.Contains(\"successfully\")) { Write-host -ForegroundColor Green \"Set key succesfully\"}\nElse { Write-host -ForegroundColor Yellow $SetKeyOutClean ; break }\n\n# Activate KMS \n$ActivateOutPut = cscript c:\\windows\\system32\\slmgr.vbs \/ato\n$ActivateOutClean = $ActivateOutPut | select -last 2 | select -First 1\n\nif ($ActivateOutClean.Contains(\"successfully\")) { Write-host -ForegroundColor Green \"Activated succesfully\"}\nElse { Write-host -ForegroundColor Yellow $ActivateOutClean ; break }\n\n\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[115,116,10],"class_list":["post-627","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-kms","tag-license","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/posts\/627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.server1.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=627"}],"version-history":[{"count":1,"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":628,"href":"https:\/\/www.server1.dk\/index.php?rest_route=\/wp\/v2\/posts\/627\/revisions\/628"}],"wp:attachment":[{"href":"https:\/\/www.server1.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.server1.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.server1.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}