I have implemented code to uninstall a msi. The problem is, when I try to uninstall it remotely using WMI it doesnt get happen and even don't throw any exception. I tried on local and found that when I run command "MsiExec.exe /x {Product Code} /qn";
in cmd as administrator the service gets uninstall & even when I try to debug code in Visual Studio (as administrator) the code works too.
But unfortunately this has to get unistall at remote machine. Is there a way where I can run this code or command as administrator remotely?. I googled and found many answers but nothing worked. Any other approach?
private void UnInstall(string ipAddress, long discoveredMachineId)
{
ipAddress = ipAddress.Trim();
ConnectionOptions connection = new ConnectionOptions();
connection.Username = this.userName;
connection.Password = this.password;
ManagementScope scope = new ManagementScope(@"\\" + ipAddress + "\\root\\CIMV2");
try
{
scope.Connect();
if (scope.IsConnected == true)
{
//Start Uninstalling
try
{
var checkKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{Product Code}");
if (checkKey == null)
{
// Key does not exist
Console.WriteLine("Not Installed");
}
else
{
// Key exist
var wmiProcess = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
ManagementBaseObject inParams = wmiProcess.GetMethodParameters("Create");
inParams["CommandLine"] = "MsiExec.exe /x {Product Code} /qn";
ManagementBaseObject outParams = wmiProcess.InvokeMethod("Create", inParams, null);
Console.WriteLine("Creation of the process returned: " + outParams["returnValue"]);
Console.WriteLine("Process ID: " + outParams["processId"]);
Console.WriteLine("UnInstalled Successfully...");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}