5

WMI を使用してアプリケーションをリモートでアンインストールするミニ w32 実行可能ファイルを作成しようとしています。

以下のコードを使用して、インストールされているすべてのアプリケーションを一覧表示できますが、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...
        }
    }

}
4

1 に答える 1

15

WMI Code Creator (Microsoft の無料ツール) をご覧ください。C# を含むさまざまな言語で WMI コードを生成できます。

メソッドの使用法を示す例を次に示しWin32_Product.Uninstallます。Win32_Productこれらはクラスの重要なプロパティであるため、アンインストールするアプリケーションの GUID、名前、およびバージョンを知る必要があります。

...

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"]);
}
于 2010-08-26T20:41:19.777 に答える