Question Using FINDSTR to filter output of a command

Cj-tech

Admirable
Jan 27, 2021
536
68
8,940
Hello! I have a section of one script that gets the last 5 of the installed product key. The goal is to run it on all machines to get an inventory of Office licenses. It works great but sometimes the output of cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus will contain multiple entries (i.e. when the user had a grace period). In that case there will be two entries... The current license and the grace period license. If the grace period is present, the script grabs that instead of the actual license. I've played around with it but can't figure out how to stop it from getting the grace period license. Any suggestions?

Code:
@echo off

for /f "tokens=2 delims=:" %%G IN ('cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus ^| findstr /C:"Last 5 characters of installed product key:"') do ( set "key=%%G" )
set "key=%key: =%"
echo %key%
 
What distinguishes a current license from a grace period license?

As a casual observation I would rather have the script grab both licenses versus neither.

However, as I understand the problem the script only grabs the grace period license - not both licenses if both licenses are present. Correct?

How are you able to tell the licenses apart? What if the script is revised to grab both types of licenses?

Maybe some sort of wild card.....

Take a close look at a group of current licenses and a group of grace period licenses.

There may be some pattern in the full product keys that distinguishes the licenses.

If so, perhaps just some small bit of additional parsing or logic that could be used distinguish between the licenses and output only current licenses.
 
What distinguishes a current license from a grace period license?

As a casual observation I would rather have the script grab both licenses versus neither.

However, as I understand the problem the script only grabs the grace period license - not both licenses if both licenses are present. Correct?

How are you able to tell the licenses apart? What if the script is revised to grab both types of licenses?

Maybe some sort of wild card.....

Take a close look at a group of current licenses and a group of grace period licenses.

There may be some pattern in the full product keys that distinguishes the licenses.

If so, perhaps just some small bit of additional parsing or logic that could be used distinguish between the licenses and output only current licenses.
It does seem like the grace period licenses have the same numbers but I think I’ve noticed ant least one of the devices has a different one. The output of the cscript "C:\Program Files\Microsoft Office\Office16\OSPP.VBS" /dstatus command gives me the last five of both license keys. The line right before the line with the actual license key on it says something like ——LICENSED—— on it, so I was trying to use that to grab the text on the immediately next line. Unfortunately I didn’t have much luck with that…
 
Two lines - correct?

Concatenate the two lines into one string.

Parse the "one string" for "LICENSED". And/or whatever variations may exist...

If "LICENSED" is found then use another VBS string function to strip out part of the string that contains the required license key either in full or just the last five characters.

I will spare you any links to string functions. :)

Very sure that you know many of the functions and can do a better job of picking the function or functions that will suit.