Compartilhar via


How to activate Windows from a script (even remotely).

I have been working on some PowerShell recently to handle the initial setup of a new machine, and I wanted to add the activation. If you do this from a command line it usually using the Software Licence manager script (slMgr.vbs) but this is just a wrapper around a couple of WMI objects which are documented on MSDN so I thought I would have a try at calling them from PowerShell. Before you make use of the code below, please understand it has had only token testing and comes with absolutely no warranty whatsoever, you may find it a useful worked example but you assume all responsibility for any damage that results to your system. If you’re happy with that, read on.  

So first, here is a function which could be written as  one line to get the status of Windows licensing. This relies on the SoftwareLicensingProduct WMI object : the Windows OS will have something set in the Partial Product Key field and the ApplicationID is a known guid. Having fetched the right object(s) it outputs the name and the status for each – translating the status ID to text using a hash table.

 $licenseStatus=@{0="Unlicensed"; 1="Licensed"; 2="OOBGrace"; 3="OOTGrace"; 
                 4="NonGenuineGrace"; 5="Notification"; 6="ExtendedGrace"} 
Function Get-Registration 

{ Param ($server="." ) 
  get-wmiObject -query  "SELECT * FROM SoftwareLicensingProduct WHERE PartialProductKey <> null
                        AND ApplicationId='55c92734-d682-4d71-983e-d6ec3f16059f'
                        AND LicenseIsAddon=False" -Computername $server | 
       foreach {"Product: {0} --- Licence status: {1}" -f $_.name , $licenseStatus[[int]$_.LicenseStatus] } 
} 

On my Windows 7 machine this comes back with Product: Windows(R) 7, Ultimate edition --- Licence status: Licensed

One of my server machines the OS was in the “Notification” state meaning it keeps popping up the notice that I might be the victim of counterfeiting  (all Microsoft shareholders are … but that’s not what it means. We found a large proportion of counterfeit windows had be sold to people as genuine.)  So the next step was to write something to register the computer. To add a licence key it is 3 lines – get a wmi object, call its “Install Product Key” method, and then call its “Refresh License Status method”.  (Note for speakers of British English, it is License with an S, even though we keep that for the verb and Licence with a C for the noun).  To Activate we get a different object (technically there might be multiple objects), and call its activate method. Refreshing the licensing status system wide and then checking the “license Status”  property for the object indicates what has happened. Easy stuff, so here’s the function.

 Function Register-Computer 
{  [CmdletBinding(SupportsShouldProcess=$True)] 
   param ([parameter()][ValidateScript({ $_ -match "^\S{5}-\S{5}-\S{5}-\S{5}-\S{5}$"})][String]$Productkey , 
          [String] $Server="."   )     $objService = get-wmiObject -query "select * from SoftwareLicensingService" -computername $server 
    if ($ProductKey) { If ($psCmdlet.shouldProcess($Server , $lStr_RegistrationSetKey)) {
                           $objService.InstallProductKey($ProductKey) | out-null  
                           $objService.RefreshLicenseStatus()         | out-null  } 

    }   get-wmiObject -query  "SELECT * FROM SoftwareLicensingProduct WHERE PartialProductKey <> null
                                                                   AND ApplicationId='55c92734-d682-4d71-983e-d6ec3f16059f'
                                                                   AND LicenseIsAddon=False" -Computername $server |      foreach-object { If ($psCmdlet.shouldProcess($_.name , "Activate product" ))                              { $_.Activate()                      | out-null                                $objService.RefreshLicenseStatus() | out-null                               $_.get()
                               If     ($_.LicenseStatus -eq 1) {write-verbose "Product activated successfully."} 
                               Else   {write-error ("Activation failed, and the license state is '{0}'" ` 
                                                      -f $licenseStatus[[int]$_.LicenseStatus] ) }
                            If     (-not $_.LicenseIsAddon) { return } 

              }               
             else { write-Host ($lStr_RegistrationState -f $lStr_licenseStatus[[int]$_.LicenseStatus]) } 
    } 
}

Things to note

  • I’ve taken advantage of PowerShell V2’s ability to include validation code as a part of the declaration of a parameter.
  • I as mentioned before, it’s really good to use the SHOULD PROCESS feature of V2 , so I’ve done that too.
  • Finally, since this is WMI it can be remoted to any computer. So the function takes a Server parameter to allow machines to be remotely activated.

A few minutes later windows detected the change and here is the result.

image

 

 

Comments

  • Anonymous
    January 01, 2003
    Er ... I'm tempted to say "3 reasons, ignorance, stupidity and 'nothing-else' " :-) Somewhere I missed  validatepattern . It is in the online help. Doh.

  • Anonymous
    July 22, 2009
    Was there a reason why you didn't use [ValidatePattern("^S{5}-S{5}-S{5}-S{5}-S{5}$")] ? jps