首页 文章

使用wmi从远程主机获取已安装的软件

提问于
浏览
-2

我想检索从远程主机安装的软件 . 我想从注册表获取详细信息,而不是从Win32_Product.I使用wmi . 我从网上尝试了很多这样的例子 . 他们中的大多数都在我需要的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 \”获取数据 .

1 回答

  • 0

    我使用下面的脚本解决了这个问题 .

    #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;
    };
    

    将上述脚本保存为“.mof”文件 . 之后,您需要使用commandprompt命令“mofcomp filename.mof”编译此脚本 . 为此,你需要拥有管理员权限 . 在编译文件之后,上面的类“InstalledSoftware”将被添加到默认根目录中的wmi类中 .

    现在,您将能够通过wmi使用类名“InstalledSoftware”访问该PC中已安装的应用程序 . 一个棘手的问题是我们需要在您需要访问已安装软件的所有远程机器上编译上述脚本 .

相关问题