Most Windows license and activation tasks can be automated using an ancient VBScript called slmgr.vbs. It does not make much sense to use this tool to get information, though, because slmgr.vbs uses localized strings and turns all data into one big string format.

A much better way is to bypass slmgr.vbs and query the original source of information directly. To find out how slmgr.vbs does this, you can look at the script source code. Run this from within the PowerShell ISE:

# get the path to the script file
$Path = Get-Command slmgr.vbs | Select-Object -ExpandProperty Source
# load the script file
$file = $psise.CurrentPowerShellTab.Files.Add($Path)

Let’s start to convert one piece of functionality to PowerShell. The parameter /dlv for example displays license information:

 
PS> slmgr.vbs /dlv
 

When you search the VBScript source code for “dlv”, you quickly find that at the heart of all of this, there is a function called GetServiceObject() which turns out to be a simple WMI query:

g_objWMIService.ExecQuery("SELECT " & strQuery & " FROM " & ServiceClass)

In fact, the script reveals the WMI classes involved in licensing. From PowerShell you can easily query all service related information like this:

 
PS> Get-WmiObject -Class SoftwareLicensingService


__GENUS                                        : 2
__CLASS                                        : SoftwareLicensingService
__SUPERCLASS                                   : 
__DYNASTY                                      : SoftwareLicensingService
__RELPATH                                      : SoftwareLicensingService.Version="10.0.17134.471"
__PROPERTY_COUNT                               : 38
__DERIVATION                                   : {}
__SERVER                                       : DESKTOP-7AAMJLF
__NAMESPACE                                    : root\cimv2
__PATH                                         : \\DESKTOP-7AAMJLF\root\cimv2:SoftwareLicensingService.Version="10.0.17134.471"
ClientMachineID                                : 
DiscoveredKeyManagementServiceMachineIpAddress : 
DiscoveredKeyManagementServiceMachineName      : 
DiscoveredKeyManagementServiceMachinePort      : 0
IsKeyManagementServiceMachine                  : 0
KeyManagementServiceCurrentCount               : 4294967295
KeyManagementServiceDnsPublishing              : True
KeyManagementServiceFailedRequests             : 4294967295
KeyManagementServiceHostCaching                : True
KeyManagementServiceLicensedRequests           : 4294967295
KeyManagementServiceListeningPort              : 1688
KeyManagementServiceLookupDomain               : 
KeyManagementServiceLowPriority                : False
KeyManagementServiceMachine                    : 
KeyManagementServiceNonGenuineGraceRequests    : 4294967295
KeyManagementServiceNotificationRequests       : 4294967295
KeyManagementServiceOOBGraceRequests           : 4294967295
KeyManagementServiceOOTGraceRequests           : 4294967295
KeyManagementServicePort                       : 1688
KeyManagementServiceProductKeyID               : 
KeyManagementServiceTotalRequests              : 4294967295
KeyManagementServiceUnlicensedRequests         : 4294967295
OA2xBiosMarkerMinorVersion                     : 1
OA2xBiosMarkerStatus                           : 1
OA3xOriginalProductKey                         : XXXXXXXXXXXXXXXXXXXXXXXXX
OA3xOriginalProductKeyDescription              : [4.0] Professional OEM
OA3xOriginalProductKeyPkPn                     : [TH]XXXXXXX
PolicyCacheRefreshRequired                     : 0
RemainingWindowsReArmCount                     : 1001
RequiredClientCount                            : 4294967295
TokenActivationAdditionalInfo                  : 
TokenActivationCertificateThumbprint           : 
TokenActivationGrantNumber                     : 4294967295
TokenActivationILID                            : 
TokenActivationILVID                           : 4294967295
Version                                        : 10.0.17134.471
VLActivationInterval                           : 4294967295
VLRenewalInterval                              : 4294967295
PSComputerName                                 : DESKTOP-7AAMJLF
 

Unlike with slmgr.vbs, you get the information in object-oriented format, so it is trivial to access individual items:

 
PS> $all = Get-WmiObject -Class SoftwareLicensingService

PS> $all.OA3xOriginalProductKeyDescription
[4.0] Professional OEM:DM
 
funcValue 1
}

There is more to discover.

Your learning points:


    • All the Windows licensing information returned by slmgr.vbs comes from WMI. Instead of calling slmgr.vbs and receiving ugly text, you can query WMI directly and bypass slmgr.vbs altogether
    • One big source of information is the SoftwareLicensingService WMI class


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Anonymous