'########################################################################### ' Script that takes two arguments and uses reg.exe to return a value for a ' specified Registry Key/Value. Handles REG_DWORD_BIG_ENDIAN correctly. ' Usage: GetRegValue REGPATH REGVALUE ' Ex: cscript GetRegValue HKLM\Software\CRTCORP\ProductA PerfCtr1 '########################################################################### ' Copyright (c) 2009 Carlos R. Tronco ' Web site - http://cars.lostroncos.org ' Version 1.0 - May 3,2009 ' ' You have a royalty-free right to use, modify, reproduce, and ' distribute this script file in any way you find useful, provided that ' you agree that the copyright owner above has no warranty, obligations, ' or liability for such use. '------------------------------------------------------------------------------- On Error Resume Next 'BTW There's very very little error-checking here.... Dim WshShell, oExec 'The Registry Key we want to look in DIM RegKey 'The registry value we want to get the value of Dim RegValue ' The actual value in RegValue Dim Result 'Set to true to get some additional output for troubleshooting. const DebugOut=False '######################################################### ' Hex to Dec : http://www.sonofsofaman.com/hobbies/code/hextodec.asp ' To help us convert values from hex to a decimal number '######################################################### Function HexToDec(strHex) dim lngResult dim intIndex dim strDigit dim intDigit dim intValue lngResult = 0 for intIndex = len(strHex) to 1 step -1 strDigit = mid(strHex, intIndex, 1) intDigit = instr("0123456789ABCDEF", ucase(strDigit))-1 if intDigit >= 0 then intValue = intDigit * (16 ^ (len(strHex)-intIndex)) lngResult = lngResult + intValue else lngResult = 0 intIndex = 0 ' stop the loop end if next HexToDec = lngResult End Function '######################################################### ' The Registry Key where the value we want is located ex: HKLM\software\fred RegKey = Wscript.Arguments(0) ' The actual Value we want to get under RegKey RegValue = WScript.arguments(1) 'Full Path to the Reg.exe util REGEXE = "C:\Windows\system32\Reg.exe" Set WshShell = CreateObject("WScript.Shell") Err.Clear 'Build our Command line, CHR(34) is double quote ' Should end up looking something like: REG Query HKLM\software\crtcorp\Product_A /z /v "Reg_Dword Example" CommandLine = REGEXE & " query " & chr(34)& RegKey & chr(34)& " /v " & chr(34) & RegValue & chr(34) If (DebugOut) Then WScript.Echo "CommandLine=[" & CommandLine & "]" 'Execute the command Set oExec = WshShell.Exec(CommandLine) If DebugOut then WScript.Echo "RC=" & Err.Number 'Let's parse the output from above.... 'Skip two header lines to get to the one we really want oExec.StdOut.SkipLine oExec.StdOut.SkipLine 'Get the line of output we do want and trim the leading and trailing spaces. Result = Trim(oExec.StdOut.ReadLine) If DebugOut then Wscript.Echo "Initial Result is [" & Result & "]" 'Cut off the RegValue at the front so we can try to figure out what kind of value this is 'ex from: DWORD_BE_Example REG_DWORD_BIG_ENDIAN 0x9e030000 'to: REG_DWORD_BIG_ENDIAN 0x9e030000 Result = Trim(Right(Result,Len(Result)-Len(RegValue))) If DebugOut then Wscript.Echo "Result is [" & Result & "]" ' Now let's split the string into parts. TmpAry(0) should be the "Type" of value TmpAry=split(Result," " ) 'ValueType should be somethling like REG_DWORD or REG_SZ or whatever Valuetype=TmpAry(0) 'LEt's Trim off the ValueType so we can get rest of line. Do this because our "value" may 'have whitespace/special chars in it. strValue= Trim(Right(Result,Len(Result)-Len(ValueType))) 'if it's DWORD or one of the hex valued types we want to trim off the leading '0x' TmpHexValue = TmpAry(Ubound(Tmpary)) HexValue = Right(TmpHexValue,Len(TmpHexValue)-2) If DebugOut then WScript.Echo "0" & TmpAry(0) WScript.Echo "Last " & TmpAry(Ubound(Tmpary)) End If 'Based on ValueType we might do different things Select Case ValueType Case "REG_DWORD" If DebugOut then WScript.ECho "Reg DWORD" 'simple straight forward hex to decimal conversion. WScript.Echo HexToDec(HexValue) Case "REG_QWORD" If DebugOut then WScript.ECho "Reg QWORD" WScript.Echo HexToDec(HexValue) Case "REG_SZ" If DebugOut then WScript.ECho "Reg SZ" WScript.Echo strValue Case "REG_MULTI_SZ" If DebugOut then WScript.ECho "Reg Multi SZ" WScript.Echo strValue Case "REG_EXPAND_SZ" If DebugOut then WScript.Echo "Expand SZ" WScript.Echo strValue Case "REG_DWORD_BIG_ENDIAN" If DebugOut then WScript.ECho "Reg DWORD BIG ENDIAN" 'need to do hex to dec translation.. after we re-arrange and pad things a bit 'Do we need to pad the value? If Len(HexValue) < 8 then If DebugOut Then WScript.Echo "Padding" WScript.Echo "String" & string(8-Len(HexValue),"0") End If 'Apparently so.... let's prefix zeros.... HexValue=string(8-Len(HexValue),"0") & HexValue If DebugOut Then WScript.Echo "New Hex Val = " & HexValue end If 'Now we need to shuffle the bytes around to get the "correct" hex # 'Temporary variable for shuffled HexValue DIM TMPHex for IDX=4 to 1 Step -1 TMPHex = TMPHEX & Mid(HexValue,(IDx-1)*2+1,2) If DebugOut Then Wscript.Echo "Mid(" & HexValue & "," & (IDX-1)*2+1 & "," & 2& ")" WScript.Echo Mid(HexValue,(IDX-1)*2+1,2) WScript.Echo "TMPHex=0x" & TmpHex End If Next WScript.Echo HexToDec(TmpHex) End Select