使用WMI远程卸载应用程序 [英] using WMI to uninstall applications remotely
问题描述
我想写一个迷你W32可执行远程卸载使用WMI的应用程序。
我可以在下面使用此代码列出所有已安装的应用程序,但我不能找到一种方法来卸载应用程序远程直通WMI和C#
我知道我可以使用MSIEXEC作为一个过程做相同的,但我想解决这个使用WMI如果可能的...
谢谢,
杰姆
静态无效RemoteUninstall(字符串应用程序的名字)
{
ConnectionOptions选项=新ConnectionOptions() ;
options.Username =管理员;
options.Password =XXX;
管理范围范围=新的管理范围(\\\\192.168.10.111\\root\\cimv2选项);
scope.Connect();
的ObjectQuery查询=新的ObjectQuery(SELECT * FROM的Win32_Product);
ManagementObjectSearcher搜索=新ManagementObjectSearcher(范围,查询);
ManagementObjectCollection queryCollection = searcher.Get();
的foreach(在queryCollection的ManagementObject米)
{
//显示远程计算机信息
Console.WriteLine(名称:{0} ,M [名称]);
如果(M [名称] ==应用程序的名字)
{
Console.WriteLine(APPNAME +发现,将被卸载......但如何);
//需要卸载此应用...
}
}
}
有一个看的 WMI代码造物主(来自微软的免费工具)MDASH;它可以生成各种语言,包括C#为您WMI代码。
下面是说明一个例子 Win32_Product.Uninstall
方法的使用。你需要知道你要卸载的应用程序的GUID,名称和版本,因为它们是的Win32_Product
类的主要属性:
...
的ManagementObject应用=
新的ManagementObject(范围,
Win32_Product.IdentifyingNumber ='{ 99052DB7-9592-4522-A558-5417BBAD48EE},名称=微软的ActiveSync',版本='4.5.5096.0',
NULL);
ManagementBaseObject outParams = app.InvokeMethod(卸载,NULL);
Console.WriteLine(卸载方法,结果是:{0},outParams [返回值]);
如果您有关于应用程序的部分信息(例如,仅名称或名称和版本),你可以使用一个 SELECT
查询,以获得相应的的Win32_Process
目标:
...
SelectQuery查询=新SelectQuery(的Win32_Product,NAME =微软的ActiveSync');
EnumerationOptions enumOptions =新EnumerationOptions();
enumOptions.ReturnImmediately = TRUE;
enumOptions.Rewindable = FALSE;
ManagementObjectSearcher搜索=新ManagementObjectSearcher(范围,查询,期权);
的foreach(在searcher.Get的ManagementObject应用程序())
{
ManagementBaseObject outParams = app.InvokeMethod(卸载,NULL);
Console.WriteLine(卸载方法,结果是:{0},outParams [返回值]);
}
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...
}
}
}
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"]);
}
这篇关于使用WMI远程卸载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!