Plugins:MSofficeKey

From OCS Inventory NG
Jump to: navigation, search


Warning: Please download the last package PluginOcsOfficekey-2.2.4.tar.gz


Retrieve Microsoft Office key

Windows Agent configuration

Create the script

Copy the file msofficekey.vbs and place it in C:\Program Files\OCS Inventory Agent\Plugins.

This script is a VB script. The name is not important. Nothing more to do.

'' Plugin for OCS Inventory NG 2.x
'' Creative Commons BY-NC-SA 3.0
'' Nicolas DEROUET (nicolas.derouet[gmail]com)
On Error Resume Next
 
Const HKEY_LOCAL_MACHINE = &H80000002
 
Dim aOffID(4,1)
aOffID(0,0) = "XP"
aOffID(0,1) = "10.0"
aOffID(1,0) = "2003"
aOffID(1,1) = "11.0"
aOffID(2,0) = "2007"
aOffID(2,1) = "12.0"
aOffID(3,0) = "2010"
aOffID(3,1) = "14.0"
aOffID(4,0) = "2013"
aOffID(4,1) = "15.0"
 
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", 64
 
Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
 
osType = 32
oReg.GetStringValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "PROCESSOR_ARCHITECTURE", osProc
If osProc = "AMD64" Then osType = 64
 
wow = ""
If osType = "64" Then wow = "WOW6432Node\"
schKey97 "SOFTWARE\" & wow & "Microsoft\"
schKey2K "Office", "SOFTWARE\" & wow & "Microsoft\Office\9.0\", Array("0000","0001","0002","0003","0004","0010","0011","0012","0013","0014","0016","0017","0018","001A","004F"), "78E1-11D2-B60F-006097C998E7"
schKey2K "Visio", "SOFTWARE\" & wow & "Microsoft\Visio\6.0\", Array("B66F45DC"), "853B-11D3-83DE-00C04F3223C8"
 
For a = LBound(aOffID, 1) To UBound(aOffID, 1)
  schKey "SOFTWARE\Wow6432Node\Microsoft\Office\" & aOffID(a,1) & "\Registration", false
  schKey "SOFTWARE\Microsoft\Office\" & aOffID(a,1) & "\Registration", true
Next
 
Sub schKey97(regKey)
  oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey & "Office\8.0", "BinDirPath", oDir97
  If IsNull(oDir97) Then Exit Sub
  oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey & "Microsoft Reference\BookshelfF\96L", "PID", oProdID
  oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey & "Windows\CurrentVersion\Uninstall\Office8.0", "DisplayName", oProd
  oInstall = "1"
  If IsNull(oProd) Then
    oInstall = "0"
    oProd = "Microsoft Office 97"
  End If
  writeXML "97",oProd,oProdID,32,"",oInstall,"",""
End Sub
 
Sub schKey2K(name, regKey, guid1, guid2)
  oProd = Null
  oInstall = "0"
  oReg.GetBinaryValue HKEY_LOCAL_MACHINE, regKey & "Registration\DigitalProductID", "", aDPIDBytes
  oKey = ""
  If Not IsNull(aDPIDBytes) Then oKey = decodeKey(aDPIDBytes)
 
  oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey & "Registration\ProductID", "", oProdID
  If IsNull(oProdID) Then Exit Sub
 
  oReg.EnumKey HKEY_LOCAL_MACHINE, "Software\" & wow & "Microsoft\Windows\CurrentVersion\Uninstall\", aKeys
  If Not IsNull(aKeys) Then
    For Each guid In aKeys
      If UCase(Right(guid,Len(guid)-InStr(guid,"-"))) = guid2 & "}" Then
        For i = LBound(guid1) To UBound(guid1)
          If UCase(Left(guid,Len(guid1(i)) + 1)) = "{" & guid1(i) Then
            oReg.GetStringValue HKEY_LOCAL_MACHINE, "Software\" & wow & "Microsoft\Windows\CurrentVersion\Uninstall\" & guid, "DisplayName", oProd
            oGUID = guid
            oInstall = "1"
          End If
        Next
      End If
    Next
  End If
 
  If IsNull(oProd) Then oProd = "Microsoft " & name & " 2000"
  writeXML "2000",oProd,oProdID,32,oGUID,oInstall,oKey,""
End Sub
 
Sub schKey(regKey, likeOS)
  oReg.GetBinaryValue HKEY_LOCAL_MACHINE, regKey, "DigitalProductID", aDPIDBytes
  If IsNull(aDPIDBytes) Then
    oReg.EnumKey HKEY_LOCAL_MACHINE, regKey, aGUIDKeys
    If Not IsNull(aGUIDKeys) Then
      For Each GUIDKey In aGUIDKeys
        schKey regKey & "\" & GUIDKey, likeOS
      Next
    End If
  Else
    oVer = aOffID(a,0)
    oProd = Null
    oKey = decodeKey(aDPIDBytes)
    oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey, "ProductID", oProdID
    oBit = osType
    If Not likeOS Then oBit = 32
    oGUID = Right(regKey,InStr(StrReverse(regKey),"\")-1)
    oInstall = "1"
    wow = ""
    If Not likeOS Then wow = "WOW6432Node\"
 
    oEdit = ""
    If (oVer = "2010" Or oVer = "2013") Then
      For i = 280 to 320 Step 2
        If aDPIDBytes(i) <> 0 Then oEdit = oEdit & Chr(aDPIDBytes(i))
      Next
    End If
    oNote = oEdit
 
    If IsNull(oProd) And (oVer = "2010" Or oVer = "2013") Then
      kEdit = UCase(oEdit)
      If Mid(oGUID,11,4) = "003D" Then kEdit = "SingleImage"
      oReg.GetStringValue HKEY_LOCAL_MACHINE, "Software\" & wow & "Microsoft\Windows\CurrentVersion\Uninstall\Office" & Left(aOffID(a,1),2) & "." & kEdit, "DisplayName", oProd
    End If
 
    If IsNull(oProd) Then _
      oReg.GetStringValue HKEY_LOCAL_MACHINE, "Software\" & wow & "Microsoft\Windows\CurrentVersion\Uninstall\" & oGUID, "DisplayName", oProd
 
    If IsNull(oProd) Then
      oInstall = "0"
      oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey, "ProductName", oProd
      If IsNull(oProd) Then oReg.GetStringValue HKEY_LOCAL_MACHINE, regKey, "ConvertToEdition", oProd
 
      ' Office Visio XP
      If IsNull(oProd) And (oVer = "XP") Then
        oReg.GetStringValue HKEY_LOCAL_MACHINE, "Software\" & wow & "Microsoft\Office\XP\Common\ProductVersion", "LastProduct", pVer
        ' Original / SP1 / SP2
        If ((pVer = "10.0.525") Or (pVer = "10.1.2514") Or (pVer = "10.2.5110")) Then
          oProd = "Microsoft Office Visio XP"
        End If
      End If
 
      ' Office Visio Viewer 2003
      If IsNull(oProd) And (oVer = "2003") And (oKey = "MF4QD-3T4PM-26X66-4KH7R-QGTYT") Then
        oProd = "Microsoft Office Visio Viewer 2003"
      End If
 
      If IsNull(oProd) Then oProd = "Unidentifiable Office " & oVer
    End If
    writeXML oVer,oProd,oProdID,oBit,oGUID,oInstall,oKey,oNote
  End If
End Sub
 
Sub writeXML(oVer,oProd,oProdID,oBit,oGUID,oInstall,oKey,oNote)
  Wscript.Echo _
  "<OFFICEPACK>" & vbCrLf & _
  "<OFFICEVERSION>" & oVer & "</OFFICEVERSION>" & vbCrLf & _
  "<PRODUCT>" & oProd & "</PRODUCT>" & vbCrLf & _
  "<PRODUCTID>" & oProdID & "</PRODUCTID>" & vbCrLf & _
  "<TYPE>" & oBit & "</TYPE>" & vbCrLf & _
  "<OFFICEKEY>" & oKey & "</OFFICEKEY>" & vbCrLf & _
  "<GUID>" & oGUID & "</GUID>" & vbCrLf & _
  "<INSTALL>" & oInstall & "</INSTALL>" & vbCrLf & _
  "<NOTE>" & oNote & "</NOTE>" & vbCrLf & _
  "</OFFICEPACK>"
End Sub
 
Function decodeKey(iValues)
  Dim arrDPID, foundKeys
  arrDPID = Array()
  foundKeys = Array()
 
  Select Case (UBound(iValues))
    Case 255:  ' 2000
      range = Array(52,66)
    Case 163:  ' XP, 2003, 2007
      range = Array(52,66)
    Case 1271: ' 2010, 2013
      range = Array(808,822)
    Case Else
      Exit Function
  End Select
 
  charset = "BCDFGHJKMPQRTVWXY2346789"
 
  For i = range(0) to range(1)
    ReDim Preserve arrDPID( UBound(arrDPID) + 1 )
    arrDPID( UBound(arrDPID) ) = iValues(i)
  Next
 
  withN = (arrDPID(UBound(arrDPID)) \ 6) And 1
  arrDPID(UBound(arrDPID)) = (arrDPID(UBound(arrDPID)) And &HF7) Or ((withN And 2) * 4)
 
  For i = 24 To 0 Step -1
    k = 0
    For j = 14 To 0 Step -1
      k = k * 256 Xor arrDPID(j)
      arrDPID(j) = k \ 24
      k = k Mod 24
    Next
    strProductKey = Mid(charset, k+1, 1) & strProductKey
  Next
 
  If (withN = 1) Then
    keypart = Mid(strProductKey,2,k)
    strProductKey = Replace(strProductKey, keypart, keypart & "N", 2, 1, 0)
    If k = 0 Then strProductKey = "N" & strProductKey
  End If
 
  decodeKey = ""
  For i = 1 To 25
    decodeKey = decodeKey & Mid(strProductKey,i,1)
    If i Mod 5 = 0 And i <> 25 Then decodeKey = decodeKey & "-"
  Next
End Function

Check the result

Result is inserted in XML file generated by agent. If you want to store this file in local, launch your agent with option /XML="C:\Documents and Settings\All Users\Application Data\OCS Inventory NG\Agent".

<OFFICEPACK>
  <OFFICEVERSION>2003</OFFICEVERSION>
  <PRODUCT>Microsoft Office Professional Edition 2003</PRODUCT>
  <PRODUCTID>55555-666-0844444-11111</PRODUCTID>
  <TYPE>32</TYPE>
  <OFFICEKEY>BBBBB-DDDDD-88888-KKKKK-66666</OFFICEKEY>
  <GUID>{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}</GUID>
  <INSTALL>1</INSTALL>
  <NOTE />
</OFFICEPACK>

Database server

Create table in database to store informations

You have to create a new table which will receive new data

mysql -u [ocs_user] -p[ocs_pass] [ocs_base] < [path]/officepack.sql
mysql -u [ocs_user] -p[ocs_pass] [ocs_base] < [path]/officepack-guid-fr.sql

Management server

Modifiy the engine

You have to modify Map.pm

linux

/usr/local/share/perl/5.10.0/Apache/Ocsinventory/

windows

c:\xampp\perl\site\lib\Apache\Ocsinventory\
#Plugin MS OFFICE PACK
	officepack => {
		mask => 0,
		multi => 1,
		auto => 1,
		delOnReplace => 1,
		sortBy => 'OFFICEVERSION',
		writeDiff => 0,
		cache => 0,
		fields => {
                        OFFICEKEY => {},
                        OFFICEVERSION => {},
                        PRODUCT => {},
                        PRODUCTID => {},
                        TYPE => {},
                        GUID => {},
                        INSTALL => {},
                        NOTE => {},
	}
},
Warning: When you will upgrade your OCS Inventory Server, Map.pm will be overwrite. Don't forget to save this file before upgrading.


Communication server

Create the workspace

First, it's important to respect files architecture. It's necessary to create a new branch in the

plugins/computer_detail directory

and named your new directory

cd_<your_module_name> 

In our case, cd_officepack. This directory will contain cd_officepack.php.

Plugin officekey 1.jpg

Then, import your 3 icons in

plugins/computer_detail/img

The name of the new icon will be in the form ms_<your_module_name>.png for inactive icon and ms_<your_module_name>_a.png for active icon. Add an icon is not an obligation because you can add your module to a menu already in place or even create a menu on a icon.


Here, icons you can use :

Cd officepack.pngCd officepack a.pngCd officepack d.png
Warning: You need to fix good permissions on cd_officekey.php.


chmod 0755 ./cd_officepack.php
chown root:www-data ./cd_officepack.php

Activate the plugin

You have to activate your plugin in

/plugins/computer_detail/cd_config.txt

Here, just the diff of this file. Mobile icon is never used, so I desactivate it to replace by my "MSofficeKey" icon

<ORDER>
.......
.......
21:cd_officepack
</ORDER>

<LBL>
.......
.......
cd_officekey:cd_officepack
</LBL>

<ISAVAIL>
.......
.......
cd_officepack:officepack
</ISAVAIL>

<URL>
.......
cd_officepack:21
.......
</URL>

Display the result in administration console

Connect to the GUI, select a machine, and click on new icon. You can see Office key and version.

Plugin officekey 3.png