使用wmi从远程主机获取安装的软件
本文关键字:获取 安装 软件 主机 wmi 程主机 使用 | 更新日期: 2023-09-27 18:18:10
我要检索从远程主机安装的软件。我想从注册表中获取详细信息,而不是从Win32_Product。我在用wmi。我已经从网上尝试了很多例子。它们中的大多数都是在c#中需要的vb.net中。谁能把代码贴出来…
这是我使用的代码
string regKeyToGet=@"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall'";
string keyToRead= "DisplayName";
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "Ravinilson";
oConn.Password = "ravi";
ManagementScope scope = new ManagementScope(@"//" + RemotePC + @"/root/default", oConn);
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
// Returns a specific value for a specified key
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
nParams["sSubKeyName"] = regKeyToGet;
inParams["sValueName"] = keyToRead;
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null);
return outParams["sValue"].ToString();
,但它给出"对象引用未设置为对象的实例"错误。我正在从"Win32_Product"安装应用程序。但它只退回windows产品。这就是为什么我想从注册表"SOFTWARE'Microsoft'Windows'CurrentVersion'Uninstall'"中获取数据。
我用下面的脚本解决了这个问题。
#PRAGMA AUTORECOVER
[dynamic, provider("RegProv"),
ProviderClsid("{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"),
ClassContext("local|HKEY_LOCAL_MACHINE''SOFTWARE''Microsoft''Windows
''CurrentVersion''Uninstall")]
class InstalledSoftware
{
[key] string KeyName;
[read, propertycontext("DisplayName")] string DisplayName;
[read, propertycontext("DisplayVersion")] string DisplayVersion;
[read, propertycontext("InstallDate")] string InstallDate;
[read, propertycontext("Publisher")] string Publisher;
};
将上述脚本保存为"。财政部"文件。之后,您需要使用命令提示符"mofcomp filename"来编译这个脚本。财政部"。为此,你需要有管理员权限。编译完文件后,上面的类"InstalledSoftware"将被添加到默认根目录下的wmi类中。
现在你将能够通过wmi使用类名"installed software"访问该pc中已安装的应用程序。一个棘手的问题是,我们需要在所有需要访问已安装软件的远程机器上编译上述脚本。