Script to produce a report of MS Office product keys with machine names

Please take a look at this webpage and specifically the extract below. Is there anyway for a script to be produced to create a report showing machine names with product keys? Additionally, for the information to be visible within Software Inventory for each machine on ITSM?

https://cwl.cc/2017/07/your-guide-to-the-confusing-my-account-microsoft-office-service.html

  • For a copy of Office you want to activate with the above product key, expand the Office entry and get the product code. You’ll need to click on the “View your product key” link and the key will expand. Keep that handy for later.
  • Open up a “Command Prompt” or “Powershell” as an administrator or (admin) user.
  • Run this command to see what product keys are currently installed: cscript “C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs” /dstatus Output: Each entry will list “Last 5 characters of installed product key: XXXXX”
  • For each key above, use this command to remove it. For two keys listed in step 3, run this command twice: cscript “C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs” /unpkey:XXXXX Output: You’ll see a success indicator.
  • Once all keys are removed, add the product key you gathered from “View your product key”: cscript “C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs” /inpkey:HGEW3-DCD22-33WSA-4325J-PUR34 Output: You’ll see a success indicator.
  • Finally, activate Office. If you’ve installed the most recent key, and your computer is on the Internet, run this: cscript “C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs” /act Output: You’ll see a success indicator, something to the effect of “Activation Successful”.
A Note: if your Office installation is 64 Bit, remove ” (x86)” from each of the commands listed above. This will point you to the right script.

Hi @nct

We will analyse and update the script request once it has been completed.

Thank you.

@nct

Here is the script for your request:

As per the commands you have provided this script has done. You have provided only for “Office16”, this script will work for all versions of Office which is installed on the particular Device for your convenience.

NOTE:

  • key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" # Give your new Product key which is valuable to activate.
Output will have Responses for each and every Commands.

Please, let us know your feedback.

key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"  # Give your new Product key which is valuable.


import os
import ctypes
import sys
import platform
import subprocess
import ctypes
import re


f='\ospp.vbs'
cmd1=' /dstatus'
cmd2=' /unpkey:'
cmd3=' /inpkey:'
cmd4=' /act'
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

def os_bits():
    y=platform.machine()
    if y=='AMD64':
        return 64
    else:
        return 32


def get_version(c):
    with disable_file_system_redirection():
        os.chdir(c)
        a=os.popen('Dir').read()
        b=re.findall('Office(.*)' ,a)
        d='office'+b[1]
    return d


def run_cmd1():
    with disable_file_system_redirection():
        i=os.popen('"'+c+'"'+cmd1).read()
        print "
"+i
        j=re.findall('key:\s(.*)' ,i)
    return j[0]

def run_cmd2():
    with disable_file_system_redirection():
        i=os.popen('"'+c+'"'+cmd2+z).read()
        print "
"+i

def run_cmd3():
    with disable_file_system_redirection():
        i=os.popen('"'+c+'"'+cmd3+key).read()
        print "
"+i


def run_cmd4():
    with disable_file_system_redirection():
        i=os.popen('"'+c+'"'+cmd4).read()
        print "
"+i


x=os_bits()
if x == 64:
    c="C:\Program Files (x86)\Microsoft Office"

else:
    c="C:\Program Files\Microsoft Office"  


y=get_version(c)
c=c+'\\'+y+f

z=run_cmd1()
run_cmd2()
run_cmd3()
run_cmd4()


Thanks, where will the output be displayed?

If you run from ITSM, Output will be on screen.

Is there no way it could be run for multiple endpoints and outputted to a file?

Oh ! That’s great idea.
We will create an output file and update in script as new one.

Thank you.

@nct

Here is the script for your request:

The Output Will be Generated in the file and The file will be stored In the Given path and Given File name.

NOTE:

  • key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" # Give your new Product key which is valuable to activate.
  • save_path="yyyyy" #Give your the output file is stored path
  • file_name ="zzz" #Give your Output file name

Output will have Responses for each and every Commands and it will be stored in the file.

Please, let us know your feedback.

key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"  # Give your new Product key which is valuable.
save_path="C:\ProgramData"
file_name="MS_Report.txt"


file_path=save_path+'\\'+file_name
def start(key,save_path,file_name,file_path):
    import os
    import ctypes
    import sys
    import platform
    import subprocess
    import ctypes
    import re
    import shutil

    file_path=save_path+'\\'+file_name
    if os.path.exists(file_path):
        os.remove(file_path)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    n=[]    
    f='\ospp.vbs'
    cmd1=' /dstatus'
    cmd2=' /unpkey:'
    cmd3=' /inpkey:'
    cmd4=' /act'

    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)

    def os_bits():
        y=platform.machine()
        if y=='AMD64':
            return 64
        else:
            return 32
    def get_version(c):
        with disable_file_system_redirection():
            os.chdir(c)
            a=os.popen('Dir').read()
            b=re.findall('Office(.*)' ,a)
            d='office'+b[1]
        return d
    def run_cmd1():
        with disable_file_system_redirection():
            i=os.popen('cscript '+'"'+c+'"'+cmd1+' > '+file_path).read()

    def run_cmd2(w):
        with disable_file_system_redirection():
          with open(w,'r') as r:
              for line in r.readlines():
                  if line:
                      line=line.strip()
                      if  line.startswith('Last'):
                          s=line
                          j=s.split('key')
                          k=s.split(':',)
                          x=k[1].strip()      
                          i=os.popen('cscript '+'"'+c+'"'+cmd2+x+' >> '+file_path).read()
                  else:
                     pass

    def run_cmd3():
        with disable_file_system_redirection():
            i=os.popen('cscript '+'"'+c+'"'+cmd3+key+' >> '+file_path).read()
    def run_cmd4():
        with disable_file_system_redirection():
            i=os.popen('cscript '+'"'+c+'"'+cmd4+' >> '+file_path).read()


    x=os_bits()
    if x == 64:
        c="C:\Program Files (x86)\Microsoft Office"

    else:
        c="C:\Program Files\Microsoft Office"  


    y=get_version(c)
    c=c+'\\'+y+f
    w=file_path
    run_cmd1()
    run_cmd2(w)
    run_cmd3()
    run_cmd4()
    print "Output file has created in the specified path:
"
    print file_path




import os
import ctypes


class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
def check():

    with disable_file_system_redirection():
        inst=os.popen("wmic product get name,identifyingnumber").read()


    return inst


inst=check()

if 'Microsoft Office' in inst:
    print "Microsoft Office is installed in the Endponit
"
    print "Activation process has Started
"
    start(key,save_path,file_name,file_path)
else:
    print "Microsoft Office is not installed in the Endpoint"