我已经实现了卸载msi的代码。问题是,当我尝试使用WMI远程卸载它时,它不会发生,甚至不会抛出任何异常。我尝试了本地,发现当我运行命令" MsiExec.exe / x {产品代码} / qn&#34 ;;在cmd中作为管理员,服务获得卸载&即使我尝试在Visual Studio中调试代码(作为管理员),代码也可以工作。
但不幸的是,这必须在远程机器上取得好成绩。有没有办法我可以远程管理员运行此代码或命令?我用Google搜索并找到了许多答案,但没有任何效果。还有其他方法吗?
private void UnInstall(string ipAddress,long discoveredMachineId)
{
ipAddress = ipAddress.Trim();
ConnectionOptions connection = new ConnectionOptions();
connection.Username = this.userName;
connection.Password = this.password;
ManagementScope范围=新的ManagementScope(@" \\" + ipAddress +" \\ root \\ CIMV2");
尝试
{
scope.Connect();
if(scope.IsConnected == true)
{
//开始卸载
尝试
{
var checkKey = Registry.LocalMachine.OpenSubKey(@" Software \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ {Product Code}");
if(checkKey == null)
{
//密钥不存在
Console.WriteLine("未安装");
}
其他
{
//密钥存在
var wmiProcess = new ManagementClass(scope,new ManagementPath(" Win32_Process"),new ObjectGetOptions());
ManagementBaseObject inParams = wmiProcess.GetMethodParameters(" Create");
inParams [" CommandLine"] =" MsiExec.exe / x {产品代码} / qn&#34 ;;
ManagementBaseObject outParams = wmiProcess.InvokeMethod(" Create",inParams,null);
Console.WriteLine("创建过程返回:" + outParams [" returnValue"]);
Console.WriteLine("进程ID:" + outParams [" processId"]);
Console.WriteLine(" UnInstalled Successfully ......");
}
}
catch(Exception ex)
{
Console.WriteLine(前);
}
}
}
catch(Exception ex)
{
Console.WriteLine(前);
}
}