Remote-IT™
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
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.
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.
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
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%
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks oDbA & bp, this is exactly what I was after, works perfectly!
ASKER
Additional feedback:
For those who might stumble across this post and find this useful as well, but also need the ability to extract the Product key from Windows 8 & 10 rather than just Windows 7. I found the following vbscript that seems to do the job well: https://winaero.com/blog/how-to-view-your-product-key-in-windows-10-windows-8-and-windows-7. It seems to be an advanced version of the getWin7Key.vbs one.
For my purposes, I've modified it according to oDbA's suggestion above for the purpose of using it through a batch file, and are able to use it on Windows 10 now as well. I've tested it on both Windows 7 & 10 and the Product keys seems to correctly match what's on the OS. Running the 'getWin7Key.vbs' vbscript on Windows 10 also produces a product key, but it's incorrect and doesn't match the actual product key, so this scripts works much better.
Here's the Modified version of the Script based on oDbA's suggestion:
Option Explicit
Dim objshell,path,DigitalID, Result
Set objshell = CreateObject("WScript.Shell")
'Set registry key path
Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
'Registry key value
DigitalID = objshell.RegRead(Path & "DigitalProductId")
Dim ProductName,ProductID,ProductKey,ProductData
'Get ProductName, ProductID, ProductKey
'ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName")
'ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID")
'ProductKey = "Installed Key: " & ConvertToKey(DigitalID)
'ProductData = ProductName & vbNewLine & ProductID & vbNewLine & ProductKey
' ********************************************
' Modification: Sets ProductKey and Echo's only the Key
ProductKey = ConvertToKey(DigitalID)
Wscript.Echo (ProductKey)
' ********************************************
'Show messbox if save to a file
'If vbYes = MsgBox(ProductData & vblf & vblf & "Save to a file?", vbYesNo + vbQuestion, "BackUp Windows Key Information") then
' Save ProductData
'End If
'Convert binary to chars
Function ConvertToKey(Key)
Const KeyOffset = 52
Dim isWin8, Maps, i, j, Current, KeyOutput, Last, keypart1, insert
'Check if OS is Windows 8
isWin8 = (Key(66) \ 6) And 1
Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) * 4)
i = 24
Maps = "BCDFGHJKMPQRTVWXY2346789"
Do
Current= 0
j = 14
Do
Current = Current* 256
Current = Key(j + KeyOffset) + Current
Key(j + KeyOffset) = (Current \ 24)
Current=Current Mod 24
j = j -1
Loop While j >= 0
i = i -1
KeyOutput = Mid(Maps,Current+ 1, 1) & KeyOutput
Last = Current
Loop While i >= 0
If (isWin8 = 1) Then
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
End If
ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5)
End Function
'Save data to a file
Function Save(Data)
Dim fso, fName, txt,objshell,UserName
Set objshell = CreateObject("wscript.shell")
'Get current user name
UserName = objshell.ExpandEnvironmentStrings("%UserName%")
'Create a text file on desktop
fName = "C:\Users\" & UserName & "\Desktop\WindowsKeyInfo.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set txt = fso.CreateTextFile(fName)
txt.Writeline Data
txt.Close
End Function
To get it to work for me, I've 'commented' out the bits in the script above that I don't need and modified it to only produce the key which seems to work well on Windows 7 and 10 that I've tested.
Modified bit:
' ********************************************
' Modification: Sets ProductKey and Echo's only the Key
ProductKey = ConvertToKey(DigitalID)
Wscript.Echo (ProductKey)
' ********************************************
The batch file then works as per oDbA's post above when calling the vbscript after saving it as GetWinProductKey.vbs.
@echo off
for /f "tokens=* " %%a in ('cscript //nologo "GetWinProductKey.vbs"' ) do (
set "ProductKey=%%a"
)
echo %ProductKey%
Guys, thanks again for your help!
Just create a folder Keys in C -->C:\Keys
Open in new window