I am trying to execute a batch file using the Process class. This code is in the middle of a larger section of code where I am using LogonUser() and WindowsIdentity.Impersonate() to impersonate the local PC admin account.
I was attempting to run a batch file within a Process, without adding credentials in ProcessStartInfo, but doing it that way caused the batch file to fail silently - no errors were raised, and expected output from the batch file never was returned (I am reading stderr and stdout asynchronously, fwiw).
I then added the credentials to ProcessStartInfo, but now I get an "Access is denied" error if I do not first call WindowsImpersonationContext.Undo(), and an "Logon failure: unknown username or bad password" error if I do call .Undo() before Process.Start(). I have triple-checked that the username/password/domain is correct, for multiple accounts.
If my code has no LogonUser() or WindowsIdentity.Impersonate() calls (and no credentials in ProcessStartInfo), then I don't have a problem with the batch file executing and output from batch file being captured.
I am able to run the batch file from the desktop successfully, either as the local admin or an arbitrary local user account. I can see its permissions show that it should be readable/executable from the accounts I am trying to run it. This is really quite the stumper; any help is appreciated.
The problem was that I needed to redirect all 3 streams; I was only redirecting 2 (out, err, not in). That basically fixed things.