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

发布于 2024-12-21 21:49:19 字数 1058 浏览 0 评论 0原文

我想检索从远程主机安装的软件。我想从注册表而不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2024-12-28 21:49:19

我通过使用下面的脚本解决了这个问题。

#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”文件。之后,您需要使用命令提示符命令“mofcomp filename.mof”来编译此脚本。为此,您需要具有管理员权限。编译文件后,上述类“InstalledSoftware”将被添加到默认根目录中的 wmi 类中。

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

I solved this problem by using the below script.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文