0

I'm writing a VBscript to inventory all the hosts info in our company network and output it to a xml which I plan accessing through php.

So far so good Wmi manager solved almost all the data I needed (Bios, Motherboard, Network Config, etc). The problem is grabbing the office key. I can print it out on cmd via ospp.vbs (a .vbs script found in microsoft office folder), but ospp needs cscript to operate (>cscript ospp.vbs /dstatus in cmd actually). This way the script print the status of the licenses, the currently office version installed and the last 5 digits of the key.

Dim ProgramPath, WshShell, ProgramArgs, WaitOnReturn,intWindowStyle
Set WshShell=CreateObject ("WScript.Shell")
ProgramPath="c:\test run script.vbs"
ProgramArgs="/hello /world"
intWindowStyle=1
WaitOnReturn=True
WshShell.Run Chr (34) & ProgramPath & Chr (34) & Space (1) & ProgramArgs,intWindowStyle, WaitOnReturn

Using this script (author: Ronnie Matthews) I'm now having a error message about using WScript instead of Cscript to run the OSPP.VBS.

Looking around again I found out this another script that should force the use of CScript:

https://gist.github.com/tuantm8/9fc1188fe915b68fc909bb534bd86a3d

Which should, at least this is what I understood, execute the first script as cscript instead of wscript as long as I inserted it after the sub call 'RunMeAsCScript' . But when I run my code, I still get the message:

"Unable to perform operation. OSPP.VBS requires cscript engine"

Can someone point me what is the problem with this code, I'm fairly new to VBScript and to be fair, not very good with programming but i'm trying to improve.

EDIT
As pointed by Lankymart, I changed the path to """C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs""" and removed all the Char (34) I had. The Cscript.exe window open and stay for the whole WScript.Sleep now but nothing is printed.

EDIT2
Just figured out what I missed, I was using the Office16 folder and we use office 2010 here, so I pointed it to the wrong place. /facepalm @Lankymart fix solved the issue, now I'll just grab the key.

This is my actual script:

RunMeAsCScript

Dim ProgramPath, wshShell, ProgramArgs, WaitOnReturn,intWindowStyle, oExec
Set wshShell = WScript.CreateObject ("WSCript.shell")
ExecPath="c:\Windows\System32\cscript.exe"
ProgramPath="""C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs"""
ProgramArgs="/dstatus"
intWindowStyle=1
WaitOnReturn=True
Set oExec = wshShell.Exec(ExecPath & Space(1) & ProgramPath & Space (1) & ProgramArgs)
Do While oExec.Status = 0
     WScript.Sleep 5000
Loop

'do whatever you want; anything after the above line you can gaurentee you'll be in cscript

Sub RunMeAsCScript()
        Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
        Set wshShell = CreateObject( "WScript.Shell" )
        strEngine = UCase( Right(WScript.FullName, 12)  )
        If strEngine <> "\CSCRIPT.EXE" Then
                ' Recreate the list of command line arguments
                strArgs = ""
                If WScript.Arguments.Count > 0 Then
                        For i = 0 To WScript.Arguments.Count
                                strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i))
                        Next
                End If
                ' Create the complete command line to rerun this script in CSCRIPT
                strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
                ' Rerun the script in CSCRIPT
                Set objDebug = wshShell.Exec( strCmd )
                ' Wait until the script exits
                Do While objDebug.Status = 0
                        WScript.Sleep 100
                Loop
                ' Exit with CSCRIPT's return code
                WScript.Quit objDebug.ExitCode
        End If
End Sub

'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
function QuoteIt(strTemp)
        if instr(strTemp," ") then
                strTemp = """" & replace(strTemp,"""","""""") & """"
        end if
        QuoteIt = strTemp
end function

It is only a copy paste of the 2 citations, as I said I'm really new into this.

Fotkurz
  • 63
  • 1
  • 6
  • 1
    In the first snippet, the `ProgramPath` should be `cscript.exe`, `ProgramArgs` should be `"""C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs"" /dstatus"`. Then you call `WshShell.Exec` instead of `WshShell.Run` and [read the output](https://stackoverflow.com/a/6073170/1682881) – Flakes Dec 10 '20 at 04:00
  • So I have tried to use the path to cscript.exe in ProgramPath and the """C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs"" /dstatus" in ProgramArgs but didn't work, then started a new Dim ExecPath which point to cscript.exe and joined it on the .Exec call together with ProgramPath (Now pointing to ospp.vbs) and ProgramArgs (/dstatus) and also made a while to oExec = 0 (As suggested in this post you linked) just so I could see the /dstatus printed out but didn't work neither. – Fotkurz Dec 10 '20 at 11:16
  • @Fotkurz just change `programPath` to `ProgramPath="""C:\Program Files (x86)\Microsoft Office\Office16\ospp.vbs"""` and get rid of the `Chr(34)` concatenation in the `wshShell.Exec` the easiest way to escape quote literals is to double them `""`, it's also more readable. – user692942 Dec 11 '20 at 10:14
  • Did the change and edited my post but it didn't work yet... It opens the cscript.exe window now tho... – Fotkurz Dec 11 '20 at 10:28

0 Answers0