다음을 통해 공유


Windows MAK activation with PowerShell

Introduction

Making deployments seamless had been lot much easier with the PowerShell. In this wiki, I would like to deal with MAK activation of Windows servers.

For MAK activation we have two methods (Phone & Internet)and if you are doing it over the Phone, this might me useful if you have the access to internet from the server that you are trying to activate. MAK activations are famous in the IaaS model cloud solutions.

This topic describes how to setup the script and the required settings:

  • The following code could be used to identify the appropriate OS type. Add/Remove as per the requirement.
  • $OSKey : Is the Product key that is a 25-character code that's used to activate Windows
01.Write-Output "Identifying OS type....."
02.$OSVer =  (Get-WmiObject -class Win32_OperatingSystem).Caption
03.Write-Output "$OSVer , Found!"
04.$OSA =  $OSVer.Replace(" ","")
05. 
06.if($OSA -eq 'MicrosoftWindowsServer2008R2Enterprise')
07.{
08.    $OSKey =  'win2k8k'
09.}
10.if($OSA -eq 'MicrosoftWindowsServer2012Standard')
11.{
12.    $OSKey =  'win2k12k'
13.}
14.if($OSA -eq 'MicrosoftWindowsServer2012R2Standard')
15.{
16.    $OSKey =  'win2k12r2k'
17.}
  • We would insert the $OSKey, then we would try to activate the OS.
  • Three attempts would be made here to activate the OS. (just to avoid internet blues)
  • If you don't require any output in the below script you may remove the Write-Host, Write-Output.
  • Don't get into a fight with me over the usage of Write-Host (just kidding!), I always wish that Write-Output also supports this color scheme and I've used it here to ease the visibility of the output.
01.try{ 
02.          
03.            #Inserting the KEY 
04.            $insertKey =  cscript c:\windows\system32\slmgr.vbs /ipk $OSkey 
05.          
06.            $retryCount =  3
07.          
08.            while ($retryCount -gt 0) 
09.            { 
10.              
11.                Write-Output "Activating License Key..."
12.  
13.                #trying to activate.... 
14.              
15.                cscript c:\windows\system32\slmgr.vbs /ato 
16.  
17.                Write-Output "Verifying Activation Status..."
18.  
19.                #checking the activation status..... 
20.  
21.                $slmgrResult =  cscript c:\windows\system32\slmgr.vbs /dli 
22.              
23.                [string]$licenseStatus =  ($slmgrResult | select-string -pattern "^License Status:") 
24.              
25.                $licenseStatus =  $LicenseStatus.Remove(0,16) 
26.  
27.                if ( $licenseStatus -match "Licensed")   
28.                { 
29.                    Write-Host "Activation Successful"  -ForegroundColor Green 
30.                      
31.                    $retryCount =  0
32.                } 
33.                  
34.                else
35.                { 
36.                    Write-Host "Activation failed."  -ForegroundColor Red 
37.                  
38.                    $retryCount =  $retryCount -  1
39.            } 
40.  
41.            if ( $retryCount -gt 0 )   
42.            { 
43.                    Write-Host "Retrying Activation. Will try $retryCount more time(s)" -ForegroundColor Yellow 
44.            } 
45.        } 
46.    } 
47.  
48.    catch 
49.    { 
50.        Write-Warning "Error during activation!" 
51.    } 
52.  
53.  }

          When the script ran successfully, the OS is activated via MAK.

Some gotchas:

  • The system should have access to internet.
  • If the traffic gets via proxy, the Proxy has to be set before this script is executed.
  • The security concern of keeping the Keys in clear text. One way to bypass this is to encrypt them with a key and keep them in a delete-after-use folder.

Script

The Script can be found in the Gallery at https://gallery.technet.microsoft.com/scriptcenter/OS-Activation-by-MAK-using-e8a8ad2c
Description:
Here is a PowerShell function that will Activate the Windows OS by MAK.The server should have internet enabled for MAK Activation to be successful.This function will activate the server using MAK activation procedure with the right MAK key for the desired OS.

Usage:
MAK-Activate

 
-key parameter is Mandatory for this function.
 
SYNTAX
    MAK-Activate -key XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
 

Example.
Activating server.

 MAK-Activate -key XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

Conclusion

Provisioning servers with the extreme customization is never this fun.

References