-1

I wrote an app in C# (WPF) which takes remote hosts data (using Psexec).

The app requires you to be with high privileges (Administrator).

I have this kind of code in my app:

var proc = new Process {
    StartInfo = new ProcessStartInfo {
        FileName = "psexec.exe",
        Arguments = "\\\\" + ip + " ipconfig",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true
    }
};
proc.Start();
if (!proc.WaitForExit(60000))
    proc.Kill();
output_error = proc.StandardError.ReadToEnd();
output_stan = proc.StandardOutput.ReadToEnd();

If i'm running the app from Visual Studio (In debug mode), i get an output, but when i'm running the app from the exe file the standard redirected output which is just empty.

Does anyone has a possible solution for this?

*The output which is redirected as an error is a standrad psexec output which says basiclly that the command worked just find (error 0).

Thx.

chenan
  • 69
  • 1
  • 1
  • 7
  • Are you running it from an elevated command prompt? I can't see anything wrong with how you are creating the process. Can you include the code that reads from the standard output? – user3690202 Aug 17 '15 at 15:00
  • Yes. I'm running from elevated command prompt. Included what you asked. – chenan Aug 17 '15 at 15:18

1 Answers1

0

From MSDN:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();

// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

In particular note how you shouldn't wait before reading to the end of the stream otherwise you might be getting deadlocks.

I have modified your code to do it this way, and the following works fine for me:

    static void Main(string[] args)
    {
        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "ping.exe",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        string output_error = proc.StandardError.ReadToEnd();
        string output_stan = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();

        Trace.TraceInformation(output_stan);
    }
user3690202
  • 3,844
  • 3
  • 22
  • 36
  • Tried this now and it didn't redirect nothing. Moreover, it changed the error output and wrote "error 1" - not sure what it means. – chenan Aug 17 '15 at 15:27
  • @chenan - I have modified your code to call ping.exe and do things the way MSDN recommends and it works. However, perhaps there is something in psexec.exe that is causing you problems. Perhaps try switch it to ping.exe and check that all the standard output capture code works properly, then we can look at what is different with psexec.exe? – user3690202 Aug 17 '15 at 15:39
  • Well i gave an example because psexec is doing the problems for me. I noticed that when i run the app from visual studio in Debug Mode it runs perfectly, but when i tried "Without debugging" it prompted me to elevate my permissions (It's running from elevated permissions already). Therefore, i realized that in Debug Mode i get an extra elevated permissions which in "Without debugging" or with running the exe i don't get (In both situations i'm running the app in administrator permissions). Do you know a way to change some property so it would be equaled to "Debug mode" permissions? – chenan Aug 17 '15 at 18:58
  • Unless you are running Visual Studio as administrator then there is no difference between running the exe with debugging or without debugging. There are no additional permissions, therefore there is no property to change to equal "debug mode" permissions. – user3690202 Aug 17 '15 at 19:13
  • Believe it or not, the difference exists. Debug mode = works fine, "Withoud debugging\direct exe" = missing output and prompting for credentials again. Maybe it's about GPO or folder security options that i need to check? – chenan Aug 17 '15 at 19:21
  • What about when you switch to release mode and run it from within Visual Studio? I don't think the difference exists, rather I suspect you are doing something wrong. Things you are saying contradict themselves, such as "Yes. I'm running from elevated command prompt." then you contradict it with " it prompted me to elevate my permissions". But if you are running an app that requires elevation from within an elevated command prompt then it should not prompt for elevation. – user3690202 Aug 17 '15 at 19:32
  • One other thing you should try is setting your program to run as elevated. Then, when it goes to start psexec, it shouldn't need to prompt for elevation. See here: http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7 – user3690202 Aug 17 '15 at 19:40