3

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);
            }
        } 
Abb
  • 3,051
  • 3
  • 17
  • 34
  • Did you check [PsExec](https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) ? You can execute msiexec remotely with it, but I think you need to specify the remote administrative credentials as well. – Oguz Ozgul Dec 04 '15 at 06:28
  • @OguzOzgul yes, i did check this, and its not safe approach – Abb Dec 04 '15 at 06:34
  • Ok. The ConnectionOptions connection; variable does not seem to be used after intialization in your source code. Should not you be passing it to ManagementScope constructor? You also check the installation only on the local machine and not on the remote. – Oguz Ozgul Dec 04 '15 at 06:39

0 Answers0