使用vbs搜索注册表以查找路径的未知部分

时间:2016-12-07 20:28:23

标签: vbscript

我使用路径来定位包含可以更改的guid的信息。我有guid值硬编码,但当它改变它不起作用。我需要动态发现guid。我在guid的另一端知道一个值,并且有一个REG查询找到整个路径,但我无法弄清楚如何捕获该路径。

这是REG查询:

REG Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products /D /V /F "Microsoft Office Professional Plus 2010" /S /E 

它返回值“DisplayName”及其内容“Microsoft Office Professional Plus”

从批处理文件运行时,它还会显示包含难以捉摸的guid的整个路径。我想从vb脚本中做到这一点。

1 个答案:

答案 0 :(得分:1)

此外,较新的Windows Scripting Host Shell对象也使注册表访问变得容易。

Set wshshell = CreateObject("WScript.Shell")

wshshell.RegDelete(strName)

wshshell.RegRead(strName) 

wshshell.RegWrite(strName, anyValue [,strType])

请参阅https://msdn.microsoft.com/en-us/library/293bt9hh(v=vs.84).aspx

WMI也可以访问注册表。与上述两种方法不同,它可以枚举,因此您可以在不必提前知道的情况下查看其中的内容。

Dim proglist()
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
ret = oReg.EnumKey(&H80000002, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", proglist)
If err.num =0 then
   For each prog in proglist
        msgbox prog
   Next
Else
    Msgbox err.num & " " & err.description & " " & err.source
    err.clear
End If

https://msdn.microsoft.com/en-us/library/aa390387(v=vs.85).aspx

它还可以检查安全性并监控密钥的更改。

这会监视对Windows卸载密钥的更改。

Set objWMIService = GetObject("winmgmts:root/default") 
Set objEvents = objWMIService.ExecNotificationQuery("SELECT * FROM RegistryTreeChangeEvent WHERE Hive='HKEY_LOCAL_MACHINE' AND RootPath='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.GetObjectText_()
Loop

https://msdn.microsoft.com/en-us/library/aa393041(v=vs.85).aspx

递归用于遍历树中的每个节点。每次遇到节点时,该函数都会调用它自己。使用cscript从程序开始,以避免几千个msgbox - cscript //nologo c:\folder\RecurseReg.vbs

Set wshshell = CreateObject("WScript.Shell")

EnumReg "SOFTWARE\CLASSES"

Sub EnumReg(RegKey)
    On Error Resume Next
    wscript.echo "---------------------------------------"
    wscript.echo "HKLM\" & RegKey & " = " & wshshell.RegRead("HKLM\" & RegKey & "\")
    err.clear
    Dim KeyList()
    Dim ValueNameList()
    Dim ValueList()
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    If err.number <> 0 then 
        wscript.echo err.number
        err.clear
    End If
    ret = oReg.EnumValues(&H80000002, RegKey, ValueNameList, ValueList)
    If err.number = 0 then
       For each valuename in ValueNameList
        If valuename <> "" then
            Value = wshshell.RegRead("HKLM\" & RegKey & "\" & valuename)
            err.clear
                wscript.echo valuename & " - " & Value
        End If
       Next
    Else
        Msgbox err.num & " " & err.description & " " & err.source
        err.clear
    End If


    ret = oReg.EnumKey(&H80000002, RegKey, Keylist)
    If err.number =0 then
       For each key in keylist
            EnumReg RegKey & "\" & key
       Next
    Else
        Msgbox err.num & " " & err.description & " " & err.source
        err.clear
    End If
End Sub

将两者放在一起(这应该是VC 2008运行时应该在所有计算机上)

Dim proglist()
Set wshshell = CreateObject("WScript.Shell")
On Error Resume Next
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
ret = oReg.EnumKey(&H80000002, "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products", proglist)
If err.num =0 then
   For each prog in proglist
'        msgbox prog
       If wshshell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\" & Prog & "\InstallProperties\DisplayName") = "Microsoft Visual C++ 2008 Redistributable - x64 9.0.30729.17" then
           Msgbox "Found " & Prog 
       End If
   Next
Else
    Msgbox err.num & " " & err.description & " " & err.source
    err.clear
End If

适用于V6或VBA

VBA中的注册表很简单。它非常有限并且使用ini文件概念。 其中有一些如(来自VBA编辑器中的对象浏览器[F2])

 Function GetAllSettings(AppName As String, Section As String)
Member of VBA.Interaction

 Sub SaveSetting(AppName As String, Section As String, Key As String, Setting As String)
Member of VBA.Interaction

 Sub DeleteSetting(AppName As String, [Section], [Key])
Member of VBA.Interaction

 Function GetSetting(AppName As String, Section As String, Key As String, [Default]) As String
Member of VBA.Interaction

也可以使用Windows API调用。

RegOpenKeyEx

RegOpenKeyEx函数打开指定的注册表项。

LONG RegOpenKeyEx(
  HKEY hKey,         // handle to open key
  LPCTSTR lpSubKey,  // subkey name
  DWORD ulOptions,   // reserved
  REGSAM samDesired, // security access mask
  PHKEY phkResult    // handle to open key
);