Search code examples
c#psexecprocess.start

Psexec works on command line but not in ASP


I am using PsExec to remotely fire a program. I can fire the actual program (not displayed here) or cmd.exe remotely with no problems whatsoever from the command line. When I try to fire it from ASP and C#, it will not trigger the command prompt, even though I am using the same exact string. Here is the string I am using that works every time, and the code that doesn't. Help please!

Working String:

C:\psexec \\10.0.0.25 -u Administrator -p password -d -i cmd.exe

Non-working code:

Process process = new Process();
        ProcessStartInfo psi = new ProcessStartInfo(@"C:\PsExec.exe")
        {
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            Arguments = @"\\10.0.0.25 -u Administrator -p password -d -i cmd.exe"
        };
        process.StartInfo = psi;
        var success = process.Start();

Solution

  • One option, assuming you have control over the machine, is to setup the psexec command as a Task Scheduler job, then execute the task scheduler job from your ASP app. You can configure the task scheduler to run as an administrator, and when you fire off the job it will run under that credentials. You won't get any output that way though, so if that's an issue there may not be a good choice.

    See How to run existing windows 7 task using command prompt for an example of running the task..