1

So I'm trying to run an .exe on my windows server that requires a user with specific access rights to run it. Luckily I have those rights on the server and can run the executable just fine manually.

However when I want to run it from my code, which is a .net core console API application I encounter a problem saying: 'The handle is invalid'.

Here is the method where Im trying to achieve this:

public void UpdateDataSets()
        {
            try
            {
                Process processStart = new Process();

                ProcessStartInfo startInfo = new ProcessStartInfo(@"PathToExecutable.exe");
                startInfo.WindowStyle = ProcessWindowStyle.Normal;

                startInfo.Arguments = $@"MyArguments";
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = false;
                startInfo.UseShellExecute = false;


                startInfo.UserName = "MyUserName";
                startInfo.Domain = "MyDomain";
                startInfo.Password = new NetworkCredential("", "MyUserPassword").SecurePassword;
                processStart.StartInfo = startInfo;

                string textToRead;

                using(Process process = Process.Start(startInfo))
                {
                    textToRead = process.StandardOutput.ReadToEnd();
                    process.WaitForExit(20000); //time limit because maybe infinite, I dont know?
                }

                File.WriteAllText(@"StandardOutput.txt", textToRead);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

I first tried with startInfo.Verb = "runas" and with startInfo.LoadUserProfile = true before hard-coding my active directory credentials, but I just got different errors there.

What am I doing wrong here?

Vrankela
  • 1,162
  • 3
  • 16
  • 39
  • Check if this thread can help: https://stackoverflow.com/questions/4624113/start-a-net-process-as-a-different-user – Fei Han Mar 30 '20 at 05:47
  • I tried that exact link @FeiHan but I get the same error: 'The handle is invalid'. Could it be because Im using .net core? – Vrankela Mar 30 '20 at 07:09

0 Answers0