近日在研究如何编程修改Windows系统中网络连接的名称,今晚用vbs脚本来测试从注册表中获取网络名称的思路方法,其中有一个注册表数值类型是REG_MULTI_SZ,用CreateObject("Wscript.Shell").RegRead()来读取后,用echo显示其值时总是提示错误:类型不匹配,代码:800A000D,尽管知道REG_MULTI_SZ类型的值是个数组,由于自己一般是用MASM32编程的,对VBScript不是很熟悉,只想到了定义一个数组来接收返回值,这样仍然不能解决问题。
于是用sogou在网上搜索解决方法。结果在中文网页没找到,用英文搜索很顺利就找到了附录参考下面两篇参考文档(如此怎能不怀念Google?),后来根据参考文档1通过wmi解决了问题,但对RegRead()还不死心,继续研究,终于在参考文档2中找到了解决方法。
下面的是分别用RegRead()和wmi两种方法来读取注册表中REG_MULTI_SZ类型数值并显示的演示代码。
转载备查。
参考文档1 转自:https://blogs.msdn.microsoft.com/alejacma/2008/04/11/how-to-read-a-registry-key-and-its-values-vbscript/
How to read a registry key and its values (VBScript)
Hi all, welcome back,
Today I’ll share with you a couple of VBScript samples I developed the other day. They use WMI and its StdRegProv class to read theWindows registry.
This sample will take a registry key and show its subkeys and the values
But what if we only need to know if a registry key exists? We could just do the following:
I hope this helps.
Cheers,
Alex (Alejandro Campos Magencio)
Tags VBScript
参考文档2 转自:https://technet.microsoft.com/en-us/library/ee156602.aspx
Reading From and Writing to the Local Registry
Microsoft® Windows® 2000 Scripting Guide
As a general rule, it is best to manage the registry using system tools such as Regedit.exe; although not foolproof, these tools have built-in safeguards that help minimize the damage that can be caused by incorrectly configuring a registry entry. On the other hand, it is also true that many of these registry tools cannot be automated and are designed to work on only one computer at a time (typically the local computer). It is one thing to say that you should use Regedit.exe to manage the registry; it is quite another to have an urgent security bulletin recommending that you change a registry entry on all 1,000 of your domain controllers as quickly as possible. In situations in which system tools are not fast enough or efficient enough, the WshShell object provides methods for reading from, writing to, and deleting from the registry.
Caution
- Changing the registry with a script can easily propagate errors. The scripting tools bypass safeguards, allowing settings that can damage your system, or even require you to reinstall Windows. Before scripting changes to the registry, test your script thoroughly and back up the registry on every computer on which you will make changes. For more information about scripting changes to the registry, see the Registry Reference on the Microsoft Windows 2000 Server Resource Kit companion CD or at Microsoft Windows 2000 Server Resource Kit.
Reading a Registry Entry
The registry is the primary configuration database for the Windows operating system; the ability of an operating system component to run, and to run correctly, often depends on the configuration of one or more settings within the registry.
As a system administrator, you spend a considerable amount of time checking values set within the registry. For example, in the event of computer problems, support personnel will often ask you to verify specific registry settings. This can be done directly, using a tool such as Regedit.exe, or it can be done programmatically, using the WshShell RegRead method.
For the most part, the RegRead method requires you to do just two things: 1) Create an instance of the WScript Shell object and 2) call the RegRead method, specifying the registry entry you wand to read. For example, the version number of the Windows operating system is stored in HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion. You can retrieve this value by using the following code:
Set objShell = WScript.CreateObject("WScript.Shell") sngVersion = objShell.RegRead _ ("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion") Wscript.Echo sngVersion
Registry Data Types
Each value stored in the registry has a particular data type. Table 3.15 lists the subset of registry types that WSH supports and the corresponding VBScript-compatible types into which the RegRead method translates corresponding registry values.
Table 3.15 Registry Data Types and Associated Script Data Types
Name | Data Type | Script Data Type |
REG_SZ | String | Converted to String |
REG_DWORD | Number | Converted to Integer |
REG_BINARY | Binary Value | Converted to VBArray of Integers |
REG_EXPAND_SZ | Expandable String | Converted to String |
REG_MULTI_SZ | Array of Strings | Converted to VBArray of Strings |
The data types listed in Table 3.15 are the ones most commonly used in the registry. If your script attempts to use the RegRead method to retrieve the value of a registry entry with an unsupported data type, the call will result in an error.
Note
- Unfortunately, WSH does not provide a way for you to verify the data type of a registry entry before you attempt to read it. However, you can use WMI to verify data types.
The script in Listing 3.27 uses the RegRead method to read the value of a multistring registry entry. Because this is a multistring value, the information is returned as an array, and a For Each loop is used to report each item in that array.
Listing 3.27 Reading a Multistring Value from the Registry
123456 | Set objShell = WScript.CreateObject("WScript.Shell")arrValues = objShell.RegRead _ ("HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Security\Sources")For Each strValue In arrValues Wscript.Echo strValueNext |
When the preceding script is run under CScript, output similar to the following is displayed in the command window:
Spooler Security Account Manager SC Manager NetDDE Object LSA DS Security
Creating or Modifying a Registry Entry
Your scripts can use the RegWrite method to create a new registry entry or modify an existing one. The RegWrite method accepts three parameters: the registry entry to create or modify, the value to assign to the entry, and (optionally) the data type of the entry.
The script in Listing 3.28 uses the RegWrite method to create a DWORD entry (and set the value to 56) in the registry.
Listing 3.28 Creating a DWORD Value in the Registry
12 | Set objShell = WScript.CreateObject("WScript.Shell")objShell.RegWrite "HKCU\TestKey\Version", 56, "REG_DWORD" |
Note
- The WshShell RegWrite method does not support writing the REG_MULTI_SZ data type.
Deleting a Registry Entry
Your scripts can use the RegDelete method to delete registry subkeys or entries. The RegDelete method accepts a single parameter that specifies the subkey or entry to delete. Deleting a subkey deletes all the entries in that subkey.
The script in Listing 3.29 uses the RegDelete method to delete a DWORD value in the registry.
Listing 3.29 Deleting a DWORD Value in the Registry
12 | Set objShell = WScript.CreateObject("WScript.Shell")objShell.RegDelete "HKCU\TestKey\Version" |