使用WMI远程卸载应用程序

发布于 2024-09-16 07:09:06 字数 1079 浏览 4 评论 0原文

我正在尝试编写一个迷你 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 技术交流群。

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

发布评论

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

评论(1

浪漫人生路 2024-09-23 07:09:06

查看 WMI 代码创建器(来自 Microsoft 的免费工具)——它可以为您生成各种语言的 WMI 代码,包括 C#。

下面的示例说明了 Win32_Product.Uninstall 方法的用法。您需要知道要卸载的应用程序的 GUID、名称和版本,因为它们是 Win32_Product 类的关键属性:

...

ManagementObject app = 
    new ManagementObject(scope, 
    "Win32_Product.IdentifyingNumber='{99052DB7-9592-4522-A558-5417BBAD48EE}',Name='Microsoft ActiveSync',Version='4.5.5096.0'",
    null);

ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);

如果您有有关应用程序的部分信息(例如,只有名称或名称)和版本),您可以使用SELECT查询来获取相应的Win32_Process对象:

...
SelectQuery query = new SelectQuery("Win32_Product", "Name='Microsoft ActiveSync'");

EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.ReturnImmediately = true;
enumOptions.Rewindable = false;

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, options);

foreach (ManagementObject app in searcher.Get())
{
    ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

    Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);
}

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 the Win32_Product class:

...

ManagementObject app = 
    new ManagementObject(scope, 
    "Win32_Product.IdentifyingNumber='{99052DB7-9592-4522-A558-5417BBAD48EE}',Name='Microsoft ActiveSync',Version='4.5.5096.0'",
    null);

ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);

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 corresponding Win32_Process object:

...
SelectQuery query = new SelectQuery("Win32_Product", "Name='Microsoft ActiveSync'");

EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.ReturnImmediately = true;
enumOptions.Rewindable = false;

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, options);

foreach (ManagementObject app in searcher.Get())
{
    ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null);

    Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文