使用WMI远程卸载应用程序
我正在尝试编写一个迷你 w32 可执行文件来使用 WMI 远程卸载应用程序。
我可以使用下面的代码列出所有已安装的应用程序,但我找不到通过 WMI 和 C# 远程卸载应用程序的方法,
我知道我可以使用 msiexec 作为进程执行相同的操作,但如果可能的话,我希望使用 WMI 解决此问题。 。
谢谢, 水泥
static void RemoteUninstall(string appname)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator";
options.Password = "xxx";
ManagementScope scope = new ManagementScope("\\\\192.168.10.111\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Product");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Name : {0}", m["Name"]);
if (m["Name"] == appname)
{
Console.WriteLine(appname + " found and will be uninstalled... but how");
//need to uninstall this app...
}
}
}
I am trying to write a mini w32 executable to remotely uninstall an application using WMI.
I can list all the installed applications using this code below but i couldnt find a way to uninstall the application remotely thru WMI and C#
I know I can do same using msiexec as a process but I wish to solve this using WMI if its possible...
Thanks,
Cem
static void RemoteUninstall(string appname)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator";
options.Password = "xxx";
ManagementScope scope = new ManagementScope("\\\\192.168.10.111\\root\\cimv2", options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Product");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Name : {0}", m["Name"]);
if (m["Name"] == appname)
{
Console.WriteLine(appname + " found and will be uninstalled... but how");
//need to uninstall this app...
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 WMI 代码创建器(来自 Microsoft 的免费工具)——它可以为您生成各种语言的 WMI 代码,包括 C#。
下面的示例说明了
Win32_Product.Uninstall
方法的用法。您需要知道要卸载的应用程序的 GUID、名称和版本,因为它们是Win32_Product
类的关键属性:如果您有有关应用程序的部分信息(例如,只有名称或名称)和版本),您可以使用
SELECT
查询来获取相应的Win32_Process
对象:Have a look at WMI Code Creator (a free tool from Microsoft) — it can generate WMI code for you in various languages, including C#.
Here's an example illustrating the
Win32_Product.Uninstall
method usage. You need to know the GUID, name and version of the application you want to uninstall, as they are the key properties of theWin32_Product
class:If you have partial info about the application (e.g. only name or name and version), you can use a
SELECT
query to obtain the correspondingWin32_Process
object: