In this section, you create a couple of registry keys that can be used to keep track of a software inventory of the workstation.
1. | Open \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs in Notepad or your favorite script editor.
|
2. | On the first line, type Option Explicit. Save your script as YourNameCreateRegistryKeys.vbs.
|
3. | Declare the following variables: strKeyPath, strComputer, objReg, subKey, arrSubKeys, and ParentKey. You code will look like the following:
Option Explicit
Dim strKeyPath
Dim strComputer
Dim objReg
Dim subKey
Dim arrSubKeys
Dim ParentKey
|
4. | Define the constant for HKLM and set it equal to &H80000002. It will look like the following:
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
|
5. | Assign the value of " SOFTWARE\INVENTORY" to the ParentKey variable. It will look like the following:
ParentKey = "SOFTWARE\INVENTORY"
|
6. | Assign the value of " SOFTWARE\INVENTORY\Conducted" to the strKeyPath variable. It looks like the following:
strKeyPath = "SOFTWARE\INVENTORY\Conducted"
|
7. | Assign the value of "." to the strComputer variable. It looks like the following:
|
8. | Use the objReg variable to hold the SWbemServicesEx object. Connect into the root\default:stdRegProv namespace on the local computer. Your code to do this looks like the following:
Set objReg=GetObject("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
|
9. | Use the createKey method of objReg to create the new registry keys. The line will need both the HKLM constant and the strKeyPath for arguments. It will look like the following:
objReg.CreateKey HKLM, strKeyPath
|
10. | Use WScript.Echo to provide feedback to the user that the key and the subkey were created. Your code could look like the following:
WScript.Echo("Created key :" & strKeyPath)
WScript.Echo("New subkey under : " & ParentKey)
|
11. | Use EnumKey to verify the existence of the newly created registry keys. EnumKey will need HKLM, ParentKey, and arrSubKeys as arguments. Use a For Each...Next loop to walk through the arrSubKeys variable. Echo out each subkey. Your code will look like the following:
objReg.EnumKey HKLM, ParentKey, arrSubKeys
For Each subKey In arrSubKeys
WScript.Echo vbTab & subKey
Next
|
12. | Save and run the script.
|
To | Do This |
---|
Run an external program in VBScript | Use the Run method or the Exec method from the WshShell object |
Use WMI to work with the registry | Use the StdRegProv WMI class |
Write a string value to the registry using the StdRegProv WMI class | Use the SetStringValue method |
Use the DeleteKey method of StdRegProv to delete a key and several subkeys from the registry | Delete the subkeys first, then delete the parent key |