Categories
开发

VBScript 注册表遍历(枚举)启动项的方法

VBScript 中对注册表的原生支持非常有限,仅仅在 WScript.Shell 下提供了读、写、删除这三个操作。不要想枚举遍历了。

Dim OperationRegistry
Set OperationRegistry = WScript.CreateObject("WScript.Shell")
'对注册表的读操作
OperationRegistry.RegRead()
'对注册表的写操作
OperationRegistry.RegWrite()
'对注册表的删操作
OperationRegistry.RegDelete()

我们来看一下通过 WMI 的变通方法。Win32_StartupCommand 中提供了本地自动启动项中所有项,有 HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE 中都具有 Run 和 RunOnce 项,Startup 文件夹、All Users Startup 文件夹等等。

实现代码,

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colStartupCommands = objWMIService.ExecQuery("Select * from Win32_StartupCommand")
For Each objStartupCommand in colStartupCommands
    Wscript.Echo "Command: " & objStartupCommand.Command
    Wscript.Echo "Description: " & objStartupCommand.Description
    Wscript.Echo "Location: " & objStartupCommand.Location
    Wscript.Echo "Name: " & objStartupCommand.Name
    Wscript.Echo "User: " & objStartupCommand.User
    Wscript.Echo
Next

这用获取到 objStartupCommand.Name 后,再可以通过 RegRead、RegWrite、RegDelete 来进行所需要的操作。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.