0

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:

            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();
ckozma
  • 49
  • 6
  • Not an answer or anything, but allowing the ASP worker to run psexec has to be the most insecure thing I've seen in quite a while.... – Heretic Monkey Jan 24 '19 at 19:53
  • Well currently it is not allowed, I suppose that is why it is failing. I am open to suggestions for running a remote application on a machine on the same network. – ckozma Jan 24 '19 at 20:20
  • 1
    You may want to consider some kind of messaging system, even if it's just a table in a shared database. When you want the process to run you add a record to the table. Then you have a separate process (perhaps on this remote machine) poll the database. When it sees the new record, it runs the process. When done it deletes the record or otherwise marks it has having been processed. – Heretic Monkey Jan 24 '19 at 20:27

3 Answers3

1

It's been a while since I was a system administrator, but if I recall correctly psexec has to be run from an administrative command prompt. Maybe the account your app is running under doesn't have rights to reach across the network and do stuff to a remote machine?

Put this in your Page_Load temporarily:

Response.Write(Environment.UserName);

and run it again, it should show you the name you're looking for at the top of your app.

Jay
  • 258
  • 2
  • 6
  • 16
  • It does fail if it is not ran under an administrative command prompt. I suppose this is why it is failing. Any suggestions to fix? I don't know how to get any output from the program to even see why it is failing. – ckozma Jan 24 '19 at 20:07
1

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..

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

Well, I am right now doing some automation and have figured out a few things. Please see below code maybe it will help you out

public static void PSExec_Method()
    {
        try
        {

            string userName = @"ABC";
            string password = "ABC";
            string remoteMachine = "ABC";

             //How to restart AppPool
            //string operation = "stop";
            //string apppoolname = "APPPOOL";
            //string command = @"%SYSTEMROOT%\System32\inetsrv\appcmd " + operation + " apppool /apppool.name:\"" + apppoolname + "\"";

            string command = @"powershell -noninteractive Get-Content C:\tmp\tmp.csv -Head 5";
            //string command = @"ipconfig";

            string PSPath = @"C:\PSTools\PsExec.exe";
            string fullcommand = PSPath + " -u " + userName + " -p " + password + " \\\\" + remoteMachine + " -h cmd.exe /c " + command;


         
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = fullcommand;
            process.Start();
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            Console.WriteLine(process.StandardError.ReadToEnd());
            process.WaitForExit();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
Faisal Ansari
  • 352
  • 2
  • 5