(view source code of readreg.vbs as plain text)
' Constants used in this script
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
Const HKCU = &H80000001 'HKEY_CURRENT_USER
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Const HKU = &H80000003 'HKEY_USERS
Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG
Const REG_SZ =1
Const REG_EXPAND_SZ =2
Const REG_BINARY =3
Const REG_DWORD =4
Const REG_MULTI_SZ =7
' Check and parse command line arguments
If WScript.Arguments.Count <> 2 Then Syntax
section = WScript.Arguments(0)
key = UCase( WScript.Arguments(1) )
sectArray = Split( section, "\", 2 )
If UBound( sectArray ) < 1 Then Syntax
hive = UCase( sectArray(0) )
tree = sectArray(1)
' Convert specified hive to associated constant
Select Case hive
Case "HKEY_CLASSES_ROOT"
hkey = HKCR
Case "HKEY_CURRENT_CONFIG"
hkey = HKCC
Case "HKEY_CURRENT_USER"
hkey = HKCU
Case "HKEY_LOCAL_MACHINE"
hkey = HKLM
Case "HKEY_USERS"
hkey = HKU
Case Else
Syntax
End Select
' Connect to the registry
'Set WshShell = WScript.CreateObject( "WScript.Shell" )
Set oReg = GetObject( "winmgmts:!root/default:StdRegProv" )
' Read and display the requested value
WScript.Echo vbCrLf & GetVal( hkey, tree, key )
'***********************************************************************
'* Function Name: GetVal *
'* Inputs: hive, tree, key *
'* Example WScript.Echo GetVal( HKCU, "Environment", "Path" ) *
'* Returns: Value in the format NAME=VALUE *
'***********************************************************************
Function GetVal( strHive, strTree, strKey )
' Read all keys and their values for the entire section into an array
If oReg.EnumValues( strHive, strTree, sKeys, iKeyType ) Then
GetVal = "-None-"
Else
For Count = 0 to UBound( sKeys )
' Select the requested key
If strKey = UCase( sKeys( Count ) ) Then
' Format the output
Select Case iKeyType( Count )
Case REG_SZ
oReg.GetStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & sValue
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & sValue
Case REG_BINARY
oReg.GetBinaryValue strHive, strTree, sKeys( Count ), aValue
GetVal = sKeys( Count ) & "=" & Join( aValue,"" )
Case REG_DWORD
oReg.GetDWORDValue strHive, strTree, sKeys( Count ), lValue
GetVal = sKeys( Count ) & "=" & lValue
Case REG_MULTI_SZ
oReg.GetMultiStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sKeys( Count ) & "=" & Join( sValue,"" )
End Select
End If
Next
GetVal = "[" & section & "]" & vbCrLf & GetVal
End If
End Function
Sub Syntax
strMsg = vbCrLf & "ReadReg.vbs, Version 1.00" _
& vbCrLf & "Read a value from the registry" _
& vbCrLf _
& vbCrLf & "Usage: READREG.VBS section key" _
& vbCrLf _
& vbCrLf & "Where: " & Chr(34) & "section" _
& Chr(34) & " is the section name, without brackets" _
& vbCrLf & " " & Chr(34) & "key" _
& Chr(34) & " is the key whose value must be read" _
& vbCrLf & vbCrLf _
& "Arguments should be enclosed in quotes if they contain spaces" _
& vbCrLf _
& vbCrLf & "Example:" & vbCrLf _
& "READREG.VBS " & Chr(34) & "HKEY_CURRENT_USER\Environment" _
& Chr(34) & " " & Chr(34) & "path" & Chr(34) _
& vbCrLf & "should return the user part of NT's PATH variable" _
& vbCrLf & vbCrLf & "Based on " & Chr(34) _
& "Registry functions Provided by the WMI StdRegProv class" _
& Chr(34) & vbCrLf & "by Andrew Mayberry" & vbCrLf _
& "http://cwashington.netreach.net/depo/view.asp?Index=560&ScriptType=vbscript" _
& vbCrLf _
& vbCrLf & "Written by Rob van der Woude" _
& vbCrLf & "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit(1)
End Sub
page last modified: 2024-04-16; loaded in 0.0121 seconds