In the second article of this series, I will continue presenting ways of using scripting with Windows Management Instrumentation methods in order to manipulate Windows registry. Note, that these methods require WMI Standard Registry provider, which is installed by default on Windows 2000 and XP platforms.
In his latest article, Marcin Policht presents additional ways of using scripting with Windows Management Instrumentation (WMI) methods in order to manipulate the Windows registry.
The first article contained examples of implementing the following methods:
GetBinaryValue - reading registry value of BINARY type
GetDWORDValue - reading registry value of DWORD type
GetExpandedStringValue - reading registry value of EXPANDED STRING type
GetMultiStringValue - reading registry value of MULTI STRING type
GetStringValue - reading registry value of STRING type
I will continue, by providing code samples showing how to take advantage of the remaining methods:
CreateKey - creates registry key
SetBinaryValue - writing registry value of BINARY type
SetDWORDValue - writing registry value of DWORD type
SetExpandedStringValue - writing registry value of EXPANDED STRING type
SetMultiStringValue - writing registry value of MULTI STRING type
SetStringValue - writing registry value of STRING type
DeleteKey - deleting registry key
DeleteValue - deleting registry value
EnumKey - enumerating registry key
EnumValues - enumerating registry value
As before, the proper execution of the code relies on having available a number of constants:
To save some space, we will assume that these five constants are defined in every one of our sample scripts (just make sure you copy them at the beginning of each).
CreateKey
The following code will create key named MyCompany in the HKEY_LOCAL_MACHINE\SOFTWARE area of the registry. The target computer name is stored in the variable sComputer. To use this script, set sComputer, hTree, and sKey variables to appropriate values (to use the local computer, you can set sComputer to a single dot (.)
sComputer = "MyComputer"
sMethod = "CreateKey"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\MyCompany"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
SetBinaryValue
In this example, I create a binary value named BinValue (located in the previously created registry key) and set it to ff 01 01 01. Note that aValue is an array of strings, each representing an integer between 0 and 255 (representing a single byte).
This time, the code creates a registry entry named DWordValue of type DWORD, located in the same key as in the previous two examples. The entry is set to decimal 11 (or 0x0000000b in hexadecimal). Note that the value is specified as an integer (but you can use any other radix that is recognized by VBScript).
Expanded string type contains system environment variables. Here is an example of a script creating such entry (using %SystemRoot% environment variable).
Multisting type consists of multiple strings. Note that when viewed in the REGEDT32 on Windows 2000, each string will appear on a separate line. REGEDIT displays them as one continous sting of characters.
EnumKey method enumerates all subkeys for the key, which name you specify. This sample script displays each subkey separately. Note, that the results are obtained by listing entries of the array stored in the sNames property (part of output parameter object).
sComputer = "MyComputer"
sMethod = "EnumKey"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
For i=0 To UBound(oOutParam.Properties_("sNames"))
WScript.Echo oOutParam.Properties_("sNames")(i)
Next
EnumValues
This method works similarly to the previous one, but instead of enumerating subkeys of a key, it performs the same for all values contained in it. The output contains also the type of each value. The types are represented as integers. In order to make the output more meaningful, I assigned appropriate name to each value and defined them as constants. The output displays each value and its corresponding data type as a single line.
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
sComputer = "MyComputer"
sMethod = "EnumValues"
hTree = HKEY_LOCAL_MACHINE
sKey = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sComputer & "/root/default:StdRegProv")
Set oMethod = oRegistry.Methods_(sMethod)
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.hDefKey = hTree
oInParam.sSubKeyName = sKey
Set oOutParam = oRegistry.ExecMethod_(sMethod, oInParam)
For i=0 To UBound(oOutParam.Properties_("sNames"))
sMessage = oOutParam.Properties_("sNames")(i)
Select Case oOutParam.Properties_("Types")(i)
Case REG_SZ sMessage = sMessage & " :REG_SZ"
Case REG_EXPAND_SZ sMessage = sMessage & " :REG_EXPAND_SZ"
Case REG_BINARY sMessage = sMessage & " :REG_BINARY"
Case REG_DWORD sMessage = sMessage & " :REG_DWORD"
Case REG_MULTI_SZ sMessage = sMessage & " :REG_MULTI_SZ"
End Select
WScript.Echo sMessage
Next
Page
1 of
1
IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
Mobile
Security
Networks/IoT
Cloud
Data Storage
Applications
Development
IT Management
Other
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...
Thanks for your registration, follow us on our social networks to keep up-to-date