在Windows中使用wmi查询查询注册表

时间:2014-05-23 07:23:39

标签: wmi registrykey

我需要在控制面板的卸载程序中显示已安装的软件。所以我使用Win32_RegistryAction类在vb脚本中查询。但我仍然无法查询特定的密钥而且它会挂起。这是我使用的一段代码。请帮助我如何使用wmi查询获取已安装的软件详细信息。
提前致谢

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_RegistryAction where key ='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\' and Root=2 ")

For Each objComputer in colSettings 
Wscript.Echo "Display Name: " & objComputer.DisplayName    
Next

2 个答案:

答案 0 :(得分:3)

您使用的是错误的WMI类,您必须使用StdRegProv类,有关如何使用此类MSDN documentation (WMI Tasks: Registry)的示例。

答案 1 :(得分:0)

我在回答自己的问题。谢谢RRUZ以正确的方式带领我。在这里,我有解决这个问题。现在我能够获得所有已安装的软件,它们在Windows中显示卸载程序。示例代码在这里给出

Dim count
Const HKEY_LOCAL_MACHINE = &H80000002
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames
count = 0
For i=0 To UBound(arrValueNames)
StrText = arrValueNames(i)
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "DisplayName",strName
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "DisplayVersion",strVersion
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "InstallLocation",strLocation
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "InstallDate",strDate
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "SystemComponent",strSystemComponent
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "WindowsInstaller",strWindowsInstaller
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "UninstallString",strUninstallString
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "ReleaseType",strReleaseType
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "ParentKeyName",strParentKeyName
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "Publisher",strPublisher
if isNull(strSystemComponent) then
if isNull(strWindowsInstaller) then
if not isNull(strUninstallString) then
if isNull(strReleaseType) then
if isNull(strParentKeyName) then
if not isNull(strName) then
printKeyValue "Win32_Product.PackageName[" & count & "]", StrName
printKeyValue "Win32_Product.Version[" & count & "]", StrVersion
printKeyValue "Win32_Product.InstallLocation[" & count & "]", strLocation
printKeyValue "Win32_Product.InstallDate[" & count & "]", strDate
printKeyValue "Win32_Product.Publisher[" & count & "]", strPublisher
printKeyValue "Win32_Product.WindowsInstaller[" & count & "]", strWindowsInstaller
printKeyValue "Win32_Product.UninstallString[" & count & "]", strUninstallString
printKeyValue "Win32_Product.ReleaseType[" & count & "]", strReleaseType
printKeyValue "Win32_Product.ParentKeyName[" & count & "]", strParentKeyName
count = count + 1
end if
end if
end if
end if
end if
end if
Next
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames
For i=0 To UBound(arrValueNames)
StrText = arrValueNames(i)
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "DisplayName",strName
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "DisplayVersion",strVersion
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "InstallLocation",strLocation
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "InstallDate",strDate
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "SystemComponent",strSystemComponent
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "WindowsInstaller",strWindowsInstaller
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "UninstallString",strUninstallString
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "ReleaseType",strReleaseType
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "ParentKeyName",strParentKeyName
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath & "\" & arrValueNames(i), "Publisher",strPublisher
if isNull(strSystemComponent) then
if isNull(strWindowsInstaller) then
if not isNull(strUninstallString) then
if isNull(strReleaseType) then
if isNull(strParentKeyName) then
if not isNull(strName) then
printKeyValue "Win32_Product.PackageName[" & count & "]", StrName
printKeyValue "Win32_Product.Version[" & count & "]", StrVersion
printKeyValue "Win32_Product.InstallLocation[" & count & "]", strLocation
printKeyValue "Win32_Product.InstallDate[" & count & "]", strDate
printKeyValue "Win32_Product.Publisher[" & count & "]", strPublisher
printKeyValue "Win32_Product.WindowsInstaller[" & count & "]", strWindowsInstaller
printKeyValue "Win32_Product.UninstallString[" & count & "]", strUninstallString
printKeyValue "Win32_Product.ReleaseType[" & count & "]", strReleaseType
printKeyValue "Win32_Product.ParentKeyName[" & count & "]", strParentKeyName
count = count + 1
end if
end if
end if
end if
end if
end if
Next
Function printKeyValue(key, value)
Wscript.Echo key & "=>" & value
End Function

此处路径 Software \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall 仅适用于Windows 7或更高版本。