I’ve been getting a lot of request for this in my trainings, so here it is.
At the end of this post, you will have all the tools you need to track your Windows product keys and Microsoft office product keys.
They way this works is we find the keys using nirsoft's keyfinder program.
Next a VBS writes the keys to the registry.
Finally we tie it all together with custom inventory rules.
Requirements:Product Finder (now form Nirsfot)
Here is the link to the portable version
http://www.nirsoft.net/utils/produkey.zip
First thing we need to do is copy this VBS code into notepad and save it as keys.vbs
Option Explicit Dim objFSO Dim objTextFile Dim winKey Dim officeKey Dim strFile Dim arrKeys Dim i Const ForReading = 1 Const winCDLine = "Windows" Const officeCDLine = "Microsoft Office" Const noViso = "Visio" Const officeKeyReg = "offKey" Const winKeyReg = "winKey" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("keys.csv", ForReading) 'Read the file into the array strFile = objTextFile.ReadAll arrKeys = Split(strFile, vbCrLf) For i = LBound(arrKeys) To UBound(arrKeys) 'try to find the windows product key If(instr(arrkeys(i),winCDLine))Then winKey = getKey(arrKeys(i)) writeReg winKey,winKeyReg End If 'try to find the office product key If(InStr(arrKeys(i),noViso)) Then ElseIf(InStr(arrKeys(i),officeCDLine)) Then officeKey = getKey(arrKeys(i)) writeReg officeKey,officeKeyReg End If Next Function getKey(strKeyLine) Dim temper Const KeyLoc = 2 Const ProdLoc = 0 temper = Split(strkeyLine,",") getKey = temper(prodloc) & "_" & temper(KeyLoc) End function Sub writeReg(strValue,StrValueName) Dim strComputer Dim oReg Dim wshShell Const HKEY_LOCAL_MACHINE = &H80000002 Const strKeyPath = "SOFTWARE\TVG" Const strKeyPath64 = "SOFTWARE\Wow6432Node\TVG" Const is64Key = "SOFTWARE\Wow6432Node" strComputer = "." Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\default:StdRegProv") Set WshShell = WScript.CreateObject("WScript.Shell") 'check for 64 bit If(OS64()) Then oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath64 oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath64,strValueName,strValue Else oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue End If End Sub Function OS64() Dim objOS Dim colItems Dim objItem Dim strComputer strComputer = "." Set objOS = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objOS.ExecQuery("SELECT OSArchitecture FROM Win32_OperatingSystem") 'this should fail on 32 bit XP On error resume next For Each objItem In colItems If(IsNull(objItem.OSArchitecture)) Then OS64 = False ElseIf(InStr(objItem.OSArchitecture,"64") > 0) Then OS64 = True Else OS64 = False End If Next On Error Goto 0 If(Err <> 0) Then OS64 = False End if End Function
Next thing we need to do is go to the scripting module and a new script.
For script type select Online Shell Script.
Choose a label, select windows as the operating system and set the script to run as local system.
Next upload produkey.cfg, produkey.exe (both found in the nirsoft folder, make sure you unzip) and keys.vbs as dependencies.
For the script text enter the following 2 lines.
ProduKey.exe /windowskeys 1 /officekeys 1 /iekeys 0 /sqlkeys 0 /exchangekeys 0 /extractedition 1 /scomma keys.csv cscript.exe keys.vbs
Last thing we need to do is change the script name from script.sh to script.bat (just below the script text).
Now that we have the keys in the registry we can start creating the custom inventory rules.
Go to the software module and add a new item.
Call the first one “Windows Product Key”
Select all your windows operating systems for supported OSs.
Finally here is the syntax for the custom inventory rule:
RegistryValueReturn(HKEY_LOCAL_MACHINE\SOFTWARE\TVG,winKey,TEXT)
Go ahead and save the software.
Next add another item but this time call it "Microsoft Office Key"
Highlight all the windows OSs again.
Here is the syntax for the second piece of software.
RegistryValueReturn(HKEY_LOCAL_MACHINE\SOFTWARE\TVG,offKey,TEXT)
At this point you could also create additional software records, for example one for each office key.
That way depending on the key you would have a unique software title. This way you could use the KBOXs built in asset management module and do licence compliance.
Below are two reports that you can use to see what machines have what keys installed.
Office report:
SELECT M.NAME, SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',1) AS OFFICE_VERSION, SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',-1) AS PRODUCT_KEY FROM SOFTWARE S, MACHINE_CUSTOM_INVENTORY MCI, MACHINE M WHERE MCI.SOFTWARE_ID = S.ID AND M.ID = MCI.ID AND S.DISPLAY_NAME = 'Microsoft Office Key' ORDER BY OFFICE_VERSION, PRODUCT_KEY
Windows report:
SELECT M.NAME, SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',1) AS WINDOWS_VERSION, SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',-1) AS PRODUCT_KEY FROM SOFTWARE S, MACHINE_CUSTOM_INVENTORY MCI, MACHINE M WHERE MCI.SOFTWARE_ID = S.ID AND M.ID = MCI.ID AND S.DISPLAY_NAME = 'Windows Product Key' ORDER BY WINDOWS_VERSION, PRODUCT_KEY
I hope this is useful.
Thanks to vacuna for helping me put this together.
SELECT M.NAME,
SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',1) AS OFFICE_VERSION,
SUBSTRING_INDEX(MCI.STR_FIELD_VALUE,'_',-1) AS PRODUCT_KEY
FROM SOFTWARE S,
MACHINE_CUSTOM_INVENTORY MCI,
MACHINE M
WHERE MCI.SOFTWARE_ID = S.ID
AND M.ID = MCI.ID
AND S.DISPLAY_NAME = 'Microsoft Office Key'
ORDER BY OFFICE_VERSION,
PRODUCT_KEY
This has a type, it needs to say 'Microsoft Product Key' or else it wont work.
Visio does not work. It is detected by Nirsoft Produkey, its just the script does not create the Reg entry as expected.
Any ideas? - talkwithphil 12 years ago
I didnt need to modify 'Microsoft Office Key' to 'Microsoft Product Key' - d-box 11 years ago
LInk to the bat to exe is below.
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
I also reworked the VBS to differentiate between Microsoft Office, Project, Visio and Operating system and it reports on all 4 of them now which is perfect.
VBS is below.
Option Explicit
Dim objFSO
Dim objTextFile
Dim winKey
Dim officeKey
Dim strFile
Dim arrKeys
Dim i
Const ForReading = 1
Const winCDLine = "Windows"
Const officeCDLine = "Microsoft Office"
Const noViso = "Visio"
Const officeKeyReg = "offKey"
Const winKeyReg = "winKey"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("keys.csv", ForReading)
'Read the file into the array
strFile = objTextFile.ReadAll
arrKeys = Split(strFile, vbCrLf)
For i = LBound(arrKeys) To UBound(arrKeys)
'try to find the windows product key
If(instr(arrkeys(i),winCDLine))Then
winKey = getKey(arrKeys(i))
writeReg winKey,winKeyReg
End If
'try to find the office product key
If(InStr(arrKeys(i),noViso)) Then
ElseIf(InStr(arrKeys(i),officeCDLine)) Then
officeKey = getKey(arrKeys(i))
writeReg officeKey,officeKeyReg
End If
Next
Function getKey(strKeyLine)
Dim temper
Const KeyLoc = 2
Const ProdLoc = 0
temper = Split(strkeyLine,",")
getKey = temper(prodloc) & "_" & temper(KeyLoc)
End function
Sub writeReg(strValue,StrValueName)
Dim strComputer
Dim oReg
Dim wshShell
Const HKEY_LOCAL_MACHINE = &H80000002
Const strKeyPath = "SOFTWARE\TVG"
Const strKeyPath64 = "SOFTWARE\Wow6432Node\TVG"
Const is64Key = "SOFTWARE\Wow6432Node"
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
Set WshShell = WScript.CreateObject("WScript.Shell")
'check for 64 bit
If(OS64()) Then
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath64
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath64,strValueName,strValue
Else
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
End If
End Sub
Function OS64()
Dim objOS
Dim colItems
Dim objItem
Dim strComputer
strComputer = "."
Set objOS = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objOS.ExecQuery("SELECT OSArchitecture FROM Win32_OperatingSystem")
'this should fail on 32 bit XP
On error resume next
For Each objItem In colItems
If(IsNull(objItem.OSArchitecture)) Then
OS64 = False
ElseIf(InStr(objItem.OSArchitecture,"64") > 0) Then
OS64 = True
Else
OS64 = False
End If
Next
On Error Goto 0
'If(Error <> 0)) Then
' OS64 = False
'End if
End Function
Also, dunno if this helps but this is the SQL for the License Checker that I run as a report. You can build it through the wizard but hey if this works as a copy paste into your environment, awesome, stop wasting time working on this.
SELECT MACHINE.NAME AS SYSTEM_NAME, USER.FULL_NAME AS FIELD_26, OS_NAME, (SELECT MACHINE_CUSTOM_INVENTORY.STR_FIELD_VALUE FROM MACHINE_CUSTOM_INVENTORY WHERE MACHINE_CUSTOM_INVENTORY.ID=MACHINE.ID AND MACHINE_CUSTOM_INVENTORY.SOFTWARE_ID=24187) AS MACHINE_CUSTOM_INVENTORY_0_24187, (SELECT MACHINE_CUSTOM_INVENTORY.STR_FIELD_VALUE FROM MACHINE_CUSTOM_INVENTORY WHERE MACHINE_CUSTOM_INVENTORY.ID=MACHINE.ID AND MACHINE_CUSTOM_INVENTORY.SOFTWARE_ID=24456) AS MACHINE_CUSTOM_INVENTORY_0_24456, (SELECT MACHINE_CUSTOM_INVENTORY.STR_FIELD_VALUE FROM MACHINE_CUSTOM_INVENTORY WHERE MACHINE_CUSTOM_INVENTORY.ID=MACHINE.ID AND MACHINE_CUSTOM_INVENTORY.SOFTWARE_ID=24305) AS MACHINE_CUSTOM_INVENTORY_0_24305, (SELECT MACHINE_CUSTOM_INVENTORY.STR_FIELD_VALUE FROM MACHINE_CUSTOM_INVENTORY WHERE MACHINE_CUSTOM_INVENTORY.ID=MACHINE.ID AND MACHINE_CUSTOM_INVENTORY.SOFTWARE_ID=24186) AS MACHINE_CUSTOM_INVENTORY_0_24186 FROM MACHINE LEFT JOIN ASSET ON ASSET.MAPPED_ID = MACHINE.ID AND ASSET.ASSET_TYPE_ID=5 LEFT JOIN USER ON USER.ID = ASSET.OWNER_ID ORDER BY MACHINE_CUSTOM_INVENTORY_0_24186 desc, MACHINE_CUSTOM_INVENTORY_0_24187 desc, FIELD_26
Cheers yall,
-Phil - talkwithphil 10 years ago
* Office 2013 (32-bit) on a 64-bit version of Windows
cscript "C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS" /dstatus
* Office 2013 (64-bit) on a 64-bit version of Windows
cscript "C:\Program Files\Microsoft Office\Office15\OSPP.VBS" /dstatus
I'm assuming that the command is the same for Office 2016, just change the directory path to reflect 2016 (e.g. Office16 instead of Office15). - jcalvo@huntonbrady.com 6 years ago
For script type select Online Shell Script.". How do I perform the aforementioned steps? I opened ProduKey.exe, ProduKey.chm, Text file, and Powershell. I couldn't find any option to create a new script or Online Shell Script. Please help! - anonymous_136850 6 years ago