Warm tip: This article is reproduced from serverfault.com, please click

Uninstall Software from Windows Service

发布于 2020-11-25 05:18:35

I have the following code in a standalone Windows Application to uninstall a program using the key from the registry. I have used this in many places in the past and it has always worked.. until now that is.

        using (Process p = new Process())
        {
            p.StartInfo.FileName = "MsiExec.exe";
            p.StartInfo.Arguments = "/x " + s.RegKey; // + " /qb";
            p.StartInfo.WorkingDirectory = @"C:\temp\";
            p.Start();
        }

It works perfectly. I then moved the same code inside a windows service as seen below. This is a KAFKA message being passed from a server app (windows app) to Kafka server and consumed by the windows service.

var readMessagesThread = new Thread(() =>
            {
                var consumerConfig = new ConsumerConfig
                {
                    GroupId = "Consumer-Group",
                    BootstrapServers = Global.KafkaServerURI,
                    MessageMaxBytes = Global.MessageMaxBytes,
                    AllowAutoCreateTopics = true,
                    FetchMaxBytes = Global.MessageMaxBytes
                };

                var cons = new ConsumerBuilder<Ignore, string>(consumerConfig).Build();
                cons.Subscribe("chat-topic");

                var cts = new CancellationTokenSource();
                Console.CancelKeyPress += (_, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                try
                {
                    while (true)
                        try
                        {
                            ConsumeResult<Ignore, string> cr = cons.Consume(cts.Token);

                            string decryptedMessage = Encryption.DecryptString(Global.AUTH_KEY, cr.Message.Value);
                            Message deserializedMessage = JsonConvert.DeserializeObject<Message>(decryptedMessage);
                            
                            switch (deserializedMessage.MessageType)
                            {
                                case Global.MessageType.UninstallSoftware:
                                
                               ******  Call function which has the same lines of code as above **
                                Break;
                            }
                        }

                        catch (ConsumeException e)
                        {
                            Console.WriteLine(e);
                        }
                }
                catch (Exception e)
                {
                    cons.Close();
                }

            });

            readMessagesThread.Start();
        }

When I run the lines of code to call MSIEXEC, nothing happens. I have tried the exact code in three different places and have played with the commands arguments over and over again.

Is it because it is in a service? Is it because it is in a thread? The service is running as LocalSystem account.

I have spend 20+ hours trying to make this work. Why does it work in one app and not another?

Any help would be greatly appreciated.

***** Nov 25 2020 927 am *** Ok, thank you for informing me of session 0... that helps. Now, since I can already interact with command prompt using PowerShell or DOS, would I be able to send that command using a process

Questioner
Chris Dunlop
Viewed
0
Chris Dunlop 2020-12-04 04:17:31