使用 wmi 从远程主机获取已安装的软件
我想检索从远程主机安装的软件。我想从注册表而不是 Win32_Product 获取详细信息。我正在使用 wmi。我已经尝试了很多网上的例子。其中大部分都在 vb.net 中,我需要在 C# 中。任何人都可以发布代码。
这是我正在使用的代码,
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\”获取数据。
I want to retrieve Softwares installed from remote host. I want to get details from the registry and not from Win32_Product.I am using wmi. I have tried so many examples from the net. Most of them are in vb.net i need in C#. can any one post the code..
This is the code i am using
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();
but it is giving "Object reference not set to an instance of an object" error.
I am getting installed applications from "Win32_Product".But it is returning only windows products.That's why i want to get data from the registry "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用下面的脚本解决了这个问题。
将以上脚本保存为“.mof”文件。之后,您需要使用命令提示符命令“mofcomp filename.mof”来编译此脚本。为此,您需要具有管理员权限。编译文件后,上述类“InstalledSoftware”将被添加到默认根目录中的 wmi 类中。
现在您将能够通过 wmi 使用类名“InstalledSoftware”访问该电脑中已安装的应用程序。一件棘手的事情是,我们需要在您需要访问已安装软件的所有远程计算机中编译上述脚本。
I solved this problem by using the below script.
Save above script as ".mof" file. After that you need to compile this script by using the commandprompt command "mofcomp filename.mof" . for this u need to have admin privileges. after compiling the file the above class "InstalledSoftware" will get added to the wmi classes in default root.
now u will be able to access installed applications in that pc using the class name "InstalledSoftware" through wmi. One tricky thing is that we need to compile the above script in all the remote machines from which u need to access installed softwares.