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.