Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readreg.vbs

(view source code of readreg.vbs as plain text)

  1. ' Constants used in this script
  2. Const HKCR = &H80000000 'HKEY_CLASSES_ROOT
  3. Const HKCU = &H80000001 'HKEY_CURRENT_USER
  4. Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
  5. Const HKU  = &H80000003 'HKEY_USERS
  6. Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG
  7. Const REG_SZ        =1
  8. Const REG_EXPAND_SZ =2
  9. Const REG_BINARY    =3
  10. Const REG_DWORD     =4
  11. Const REG_MULTI_SZ  =7
  12.  
  13. ' Check and parse command line arguments
  14. If WScript.Arguments.Count <> 2 Then Syntax
  15. section   = WScript.Arguments(0)
  16. key       = UCase( WScript.Arguments(1) )
  17. sectArray = Split( section, "\", 2 )
  18. If UBound( sectArray ) < 1 Then Syntax
  19. hive      = UCase( sectArray(0) )
  20. tree      = sectArray(1)
  21.  
  22. ' Convert specified hive to associated constant
  23. Select Case hive
  24. 	Case "HKEY_CLASSES_ROOT"
  25. 		hkey = HKCR
  26. 	Case "HKEY_CURRENT_CONFIG"
  27. 		hkey = HKCC
  28. 	Case "HKEY_CURRENT_USER"
  29. 		hkey = HKCU
  30. 	Case "HKEY_LOCAL_MACHINE"
  31. 		hkey = HKLM
  32. 	Case "HKEY_USERS"
  33. 		hkey = HKU
  34. 	Case Else
  35. 		Syntax
  36. End Select
  37.  
  38. ' Connect to the registry
  39. 'Set WshShell = WScript.CreateObject( "WScript.Shell" )
  40. Set oReg = GetObject( "winmgmts:!root/default:StdRegProv" )
  41.  
  42. ' Read and display the requested value
  43. WScript.Echo vbCrLf & GetVal( hkey, tree, key )
  44.  
  45.  
  46. '***********************************************************************
  47. '* Function Name:   GetVal                                             *
  48. '* Inputs:          hive, tree, key                                    *
  49. '* Example          WScript.Echo GetVal( HKCU, "Environment", "Path" ) *
  50. '* Returns:         Value in the format NAME=VALUE                     *
  51. '***********************************************************************
  52.  
  53. Function GetVal( strHive, strTree, strKey )
  54. 	' Read all keys and their values for the entire section into an array
  55. 	If oReg.EnumValues( strHive, strTree, sKeys, iKeyType ) Then
  56. 		GetVal = "-None-"
  57. 	Else
  58. 		For Count = 0 to UBound( sKeys )
  59. 			' Select the requested key
  60. 			If strKey = UCase( sKeys( Count ) ) Then
  61. 				' Format the output
  62. 				Select Case iKeyType( Count )
  63. 					Case REG_SZ
  64. 						oReg.GetStringValue strHive, strTree, sKeys( Count ), sValue
  65. 						GetVal = sKeys( Count ) & "=" & sValue
  66. 					Case REG_EXPAND_SZ
  67. 						oReg.GetExpandedStringValue strHive, strTree, sKeys( Count ), sValue
  68. 						GetVal = sKeys( Count ) & "=" & sValue
  69. 					Case REG_BINARY
  70. 						oReg.GetBinaryValue strHive, strTree, sKeys( Count ), aValue
  71. 						GetVal = sKeys( Count ) & "=" & Join( aValue,"" )
  72. 					Case REG_DWORD
  73. 						oReg.GetDWORDValue strHive, strTree, sKeys( Count ), lValue
  74. 						GetVal = sKeys( Count ) & "=" & lValue
  75. 					Case REG_MULTI_SZ
  76. 						oReg.GetMultiStringValue strHive, strTree, sKeys( Count ), sValue
  77. 						GetVal = sKeys( Count ) & "=" & Join( sValue,"" )
  78. 				End Select
  79. 			End If
  80. 		Next
  81. 		GetVal = "[" & section & "]" & vbCrLf & GetVal
  82. 	End If
  83. End Function
  84.  
  85.  
  86. Sub Syntax
  87. 	strMsg = vbCrLf & "ReadReg.vbs,  Version 1.00" _
  88. 	       & vbCrLf & "Read a value from the registry" _
  89. 	       & vbCrLf _
  90. 	       & vbCrLf & "Usage:  READREG.VBS  section  key" _
  91. 	       & vbCrLf _
  92. 	       & vbCrLf & "Where:               " & Chr(34) & "section" _
  93. 	       & Chr(34) & "  is the section name, without brackets" _
  94. 	       & vbCrLf & "                     " & Chr(34) & "key" _
  95. 	       & Chr(34) & "      is the key whose value must be read" _
  96. 	       & vbCrLf & vbCrLf _
  97. 	       & "Arguments should be enclosed in quotes if they contain spaces" _
  98. 	       & vbCrLf _
  99. 	       & vbCrLf & "Example:" & vbCrLf _
  100. 	       & "READREG.VBS " & Chr(34) & "HKEY_CURRENT_USER\Environment" _
  101. 	       & Chr(34) & " " & Chr(34) & "path" & Chr(34) _
  102. 	       & vbCrLf & "should return the user part of NT's PATH variable" _
  103. 	       & vbCrLf & vbCrLf & "Based on " & Chr(34) _
  104. 	       & "Registry functions Provided by the WMI StdRegProv class" _
  105. 	       & Chr(34) & vbCrLf & "by Andrew Mayberry" & vbCrLf _
  106. 	       & "http://cwashington.netreach.net/depo/view.asp?Index=560&ScriptType=vbscript" _
  107. 	       & vbCrLf _
  108. 	       & vbCrLf & "Written by Rob van der Woude" _
  109. 	       & vbCrLf & "http://www.robvanderwoude.com"
  110. 	WScript.Echo strMsg
  111. 	WScript.Quit(1)
  112. End Sub
  113.  

page last modified: 2024-04-16; loaded in 0.0121 seconds