Use Vbscript to obtain whether a value exists in the registry.

Source: Internet
Author: User

Q:
Hi, scripting guy! How do I know if a value exists in the registry on a remote computer?
-- Al
A:
Hi, Al. Time of Full disclosure: although we call it scripting guy, this does not mean that we understand everything about scripting. This is a good example. When we first saw this problem, we thought: "Well, there is obviously some if exists method in WMI to achieve this purpose ." But when we find that there is no such method, do we know how surprised we are? In fact, we cannot find any method to check whether a value exists in the registry. We are even ashamed! -- Check the document to find out what we may have missed. Of course, luck is not that good.
After we couldn't find the if exists method, we thought, "Okay, let's try to read this value. Of course, an error is triggered when you try to read a value that does not exist. Then we can capture this error and determine whether 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 local computers. You cannot use this method for remote computers. Therefore, You Need To Use WMI. Then, let's guess what will happen: if you try to read a nonexistent Value Using WMI, there will be no errors at all. On the contrary, WMI continues to run as if there were no errors. 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 check the actual value of this output parameter, we find that if the value you are trying to read does not exist, then you will get null. That is to say, all we need to do is check the null value so that we can know that the registry value does not exist.
Do you understand? The following sample script finds the registry value HKLM \ Software \ Microsoft \ Windows NT \ CurrentVersion \ test value: CopyCode The Code is 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 then we will use this value to connect to the HKEY_LOCAL_MACHINE Section of the Registry. Then, we connect to the WMI Service and connect to the stdregprov class (in Root \ default namespace ).
We first hide the Registry path and registry value 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); variable strvaluename (representing 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.
Based on the following situations, we can know whether the registry value exists: If strvalue is null, this value does not exist. We use this method for many registry values. It seems that this method is valid every time. It is also valid for REG_DWORD values. Of course, you must use the getdwordvalue method to read this value. For more information about How to Use WMI to process the registry, see the Registry chapter in Microsoft Windows 2000 scripting.
Now, we know what you are thinking: you are thinking, "Okay, what if I create a blank registry value? Is the output parameter still returned as null, even if the registry value exists ?" Believe it or not, the situation is different. Conversely, the empty registry value is returned as "empty", that is, the value is equal to the Null String (""). We realize that this problem may cause troubles. In this case, none (Null String) is not equal to none (null value ). Blank indicates that the registry value exists, but the value is blank. null indicates that the value does not exist.
As mentioned above, we have tried this method for various types of registry values at will, and it seems that this method works every time. If this method is invalid, please let us know.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.