Avatar of Remote-IT™
Remote-IT™
Flag for Australia

asked on 

echo vbscript output from batch file

I found the following vbscript below that can extract the Windows Product key from a Windows 7 machines. This works well for me as I do NOT wish to use a 3rd party tool for this purpose. My problem is that I need the output of the vbscript (which is displayed in a message box), echo'ed from the batch file. Just to clarify, the main file will be a batch file which then calls this vbscript and echo's the Product Key from the batch file (The reason I need to do this from a batch file is because this portion of the batch file is part of a much larger portion of the same batch file that get executed from a Remote Management System when then extracts system details from the machines and populate UDF fields).

Source of vbscript
https://gist.github.com/eyecatchup/d577a2628666a0ad1375

Actual VB code
' VBS Script to get the Windows(R) 7 Product Key from a PC's registry.
'
' Save the VBScript as "getWin7Key.vbs" somewhere on your Windows7 PC.
' Now, when you double-click the local script file an alertbox pops up
' displaying the product key stored in the machine's Windows registry.

Set WshShell = WScript.CreateObject("WScript.Shell")

KeyPath = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"
MsgBox ExtractKey(WshShell.RegRead(KeyPath))

Function ExtractKey(KeyInput)
    Const KeyOffset = 52
    i = 28
    CharWhitelist = "BCDFGHJKMPQRTVWXY2346789"
    Do
        Cur = 0
        x = 14
        Do
            Cur = Cur * 256
            Cur = KeyInput(x + KeyOffset) + Cur
            KeyInput(x + KeyOffset) = (Cur \ 24) And 255
            Cur = Cur Mod 24
            x = x -1
        Loop While x >= 0
        i = i -1
        KeyOutput = Mid(CharWhitelist, Cur + 1, 1) & KeyOutput
        If (((29 - i) Mod 6) = 0) And (i <> -1) Then
            i = i -1
            KeyOutput = "-" & KeyOutput
        End If
    Loop While i >= 0
    ExtractKey = KeyOutput
End Function

Open in new window


Sample Batch file:
This is a sample of the type of function I need. I am only posting this small portion as it's part of a much larger portion as mentioned earlier.

@echo off
for /f "tokens=2 delims=: " %%a in (
    'cscript //nologo "GetProductKeyWin7.vbs" | find ""' 
) do set "ProductKey=%%a"
echo %ProductKey%

Open in new window


In Summary, I suspect I need help with both scripts as the vbscript probably needs to be modified to output the ProductKey in a different way rather than a message box, and then hopefully the batch file script might find the output and display it when echo-ing it. I say echo, for the sake of testing purposes, but more accurately, I need the ProductKey 'set' in a variable in the batch file.

FYI, I have limited batch file knowledge, and no vbscript knowledge.

Thanks for any assistance that can be provided.
VB ScriptWindows BatchWindows OSWindows 7

Avatar of undefined
Last Comment
Remote-IT™

8/22/2022 - Mon