简体   繁体   中英

Uninstall Software from Windows Service - Why can I not call MSIEXE.EXE from A Windows Service

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM