VBScript读取注册表中REG_MULTI_SZ类型数值的两种方法

  近日在研究如何编程修改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类型数值并显示的演示代码。


'Author:PurpleEndurer
'  Blog:http://blog.csdn.net/purpleendurer
'  Date:2016-12-31
'   Dev:Windows XP SP3 
set ws = CreateObject("wscript.shell")

wscript.echo "1.使用RegRead()读取并显示注册表HKLM\SYSTEM\ControlSet002\Services\Tcpip\Linkage\Route数值"
a = ws.RegRead("HKLM\SYSTEM\ControlSet002\Services\Tcpip\Linkage\Route")
For Each strValue In a
 Wscript.Echo strValue
Next

wscript.echo "2.使用wmi读取并显示注册表HKLM\SYSTEM\ControlSet002\Services\Tcpip\Linkage\Route数值"

Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003
Const REG_SZ        = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY    = 3
Const REG_DWORD     = 4
Const REG_MULTI_SZ  = 7

strComputer = "." 'Use . for current machine

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
hDefKey = HKEY_LOCAL_MACHINE
strSubKeyPath = "SYSTEM\ControlSet002\Services\Tcpip\Linkage"
strValueName = "Route"
oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues
For Each strValue in arrValues
  wscript.echo strValue
Next

wscript.quit


转载备查。


参考文档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 within those subkeys:

‘ Constants (taken from WinReg.h)

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7

‘ Chose computer name, registry tree and key path

strComputer = “.” ‘ Use . for current machine
hDefKey = HKEY_LOCAL_MACHINE
strKeyPath = “SOFTWARE\Microsoft\Cryptography\Defaults\Provider”

‘ Connect to registry provider on target machine with current user

Set oReg = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\default:StdRegProv”)

‘ Enum the subkeys of the key path we’ve chosen

oReg.EnumKey hDefKey, strKeyPath, arrSubKeys

For Each strSubkey In arrSubKeys

‘ Show the subkey

wscript.echo strSubkey

‘ Show its value names and types

strSubKeyPath = strKeyPath & “\” & strSubkey
oReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes

For i = LBound(arrValueNames) To UBound(arrValueNames)
strValueName = arrValueNames(i)
Select Case arrTypes(i)

‘ Show a REG_SZ value

Case REG_SZ
oReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue
wscript.echo ” ” & strValueName & ” (REG_SZ) = ” & strValue

‘ Show a REG_EXPAND_SZ value

Case REG_EXPAND_SZ
oReg.GetExpandedStringValue hDefKey, strSubKeyPath, strValueName, strValue
wscript.echo ” ” & strValueName & ” (REG_EXPAND_SZ) = ” & strValue

‘ Show a REG_BINARY value

Case REG_BINARY
oReg.GetBinaryValue hDefKey, strSubKeyPath, strValueName, arrBytes
strBytes = “”
For Each uByte in arrBytes
strBytes = strBytes & Hex(uByte) & ” ”
Next
wscript.echo ” ” & strValueName & ” (REG_BINARY) = ” & strBytes

‘ Show a REG_DWORD value

Case REG_DWORD
oReg.GetDWORDValue hDefKey, strSubKeyPath, strValueName, uValue
wscript.echo ” ” & strValueName & ” (REG_DWORD) = ” & CStr(uValue)

‘ Show a REG_MULTI_SZ value

Case REG_MULTI_SZ
oReg.GetMultiStringValue hDefKey, strSubKeyPath, strValueName, arrValues
wscript.echo ” ” & strValueName & ” (REG_MULTI_SZ) =”
For Each strValue in arrValues
wscript.echo ” ” & strValue
Next

End Select
Next

Next


But what if we only need to know if a registry key exists? We could just do the following:

‘ Constants (taken from WinReg.h)

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

‘ Chose computer name, registry tree and key path

strComputer = “.” ‘ Use . for current machine
hDefKey = HKEY_LOCAL_MACHINE
strKeyPath = “SOFTWARE\Microsoft\Cryptography\Defaults\Provider”

‘ Connect to registry provider on target machine with current user

Set oReg = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\default:StdRegProv”)

‘ Try to enum the subkeys of the key path we’ve chosen. We can’t if the key doesn’t exist

If oReg.EnumKey(hDefKey, strKeyPath, arrSubKeys) = 0 Then
wscript.echo “Key exists!”
Else
wscript.echo “Key does not exists!”
End If


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

   
1
2
3
4
5
6
Set objShell = WScript.CreateObject("WScript.Shell")
arrValues = objShell.RegRead _
 ("HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Security\Sources")
For Each strValue In arrValues
 Wscript.Echo strValue
Next

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

   
1
2
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

   
1
2
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.RegDelete "HKCU\TestKey\Version"


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值