Ask:
Hey, scripting guy!. How do I know if a value exists in the registry on a remote computer?
--AL
For:
Hi, AL. Full disclosure time: Although we're called Scripting Guy, that doesn't mean we know anything about scripting. The question is a good example. The first time we saw this, we thought: "Well, there's obviously some sort of If Exists method in WMI that can do that." "But when we find out that there is no such way, do you know how surprised we are?" In fact, we can't find any way to check whether a value exists in the registry. We were even--ashamed!—— to consult the document, thinking we might have missed something. Luck is certainly not that good.
After we couldn't find the If Exists method, we thought: "Well, then we'll try to read this value." Of course, trying to read a nonexistent value triggers an error. Then we can catch the error and determine if the value exists. ”
As you know, if you use the WSH RegRead method to read a nonexistent value, an error is triggered. However, the RegRead method is only valid for the local computer, and you cannot use this method on a remote computer. So you need to use WMI. Then, guess what happens: if you use WMI to try to read a nonexistent value, no error will occur. Instead, WMI continues to run as if no errors occurred. It's hopeless.
But then we found something. When you use WMI to read the registry, this value is returned as an "output parameter." When we examine the actual value of this output parameter, we find that if the value you are trying to read does not exist, you will get Null. This means that all we have to do is check the Null value so that we can know that the registry value does not exist.
Do you understand? The following example script finds the registry value HKLM\Software\Microsoft\Windows Nt\currentversion\test value:
Copy Code code as follows:
const hkey_local_machine = &h80000002
strcomputer = "."
Set objregistry = getobject ("winmgmts:\\" & _
strComputer & "\root\default:StdRegProv")
strkeypath = " Software\microsoft\windows nt\currentversion "
strvaluename = " Test value "
objregistry.getstringvalue hkey_local_machine,strkeypath,strvaluename,strvalue
If IsNull (strvalue) Then
Wscript.Echo The registry key does not exist. "
else
Wscript.Echo "the registry key exists."
end if
As you can see, we first set the constant HKEY_LOCAL_MACHINE to &H80000002;, and we will use this value to connect the HKEY_LOCAL_MACHINE part of the registry. We then connect to the WMI service and connect the StdRegProv class (located in Root\default namespace).
We first hide the registry path and registry values in a pair of variables, and then call the GetStringValue method. Note the following four parameters for this method: our constant HKEY_LOCAL_MACHINE variable strpath (representing the registry path), the variable strValueName (which represents the registry value we want to read), and strvalue. strvalue is our output parameter: The actual string value recorded in the registry will be stored in strvalue and returned to us.
We can tell whether a registry value exists if strvalue is Null if the value is not present. We use this method for many registry values, and it seems to work every time. It is also valid for REG_DWORD values, and of course you must use the GetDWORDValue method to read this value. For more information about how to work with the registry using WMI, see the "registry" chapter in "Microsoft Windows 2000 scripting."
Now, we know what you're thinking: You're thinking, "Well, what if I created a blank registry value?" Does the output parameter still return as Null even if the registry value exists? "Believe it or not, the situation is different. Instead, the blank registry value is returned as "empty", which means that the value is equal to the empty string (""). We recognize that this problem can be confusing, in which case no (empty string) is not equal to none (null value). Whitespace means that the registry value exists, but the value is blank, and Null means that the value does not exist.
As mentioned above, we randomly try to use this method for various types of registry values, which seems to work every time. If you encounter a situation where this method is not valid, please let us know.