I'm facing on a crazy problem.

Using PsExec to run batch file remotely on server. When I use the same command argument in command line, all is fine.

Here is my batch file:

ECHO %USER% >> "D:\Temp\TextFile.txt"

Here is my call to remote server:

var cmdFileOnRemoteServer = "D:\Temp\myBatchFile.cmd"
var cmdExe = @"C:\Exe\PsExec.exe";
var cmdArgs = string.Format(@"\\{0} -accepteula -u {1} -p {2} {3} {4}", serverName, userName, passWord, cmdFileOnRemoteServer, utilisateur;

 proc = new Process();
 proc.StartInfo.UseShellExecute = false;
 proc.StartInfo.RedirectStandardOutput = true;
 proc.StartInfo.RedirectStandardError = true;
 proc.StartInfo.FileName = cmdExe;
 proc.StartInfo.Arguments = cmdArgs;
 var exitCode = proc.ExitCode;

exitCode = 0 in all time I'm using Asp.NET MVC app using application pool. I already use all option of PsExec. I grand all privilege on remote server to application pool which run a website. Website in installed in server which use PsExec.

All help will be appreciated. Thanks.

  • try it without the Redirects for output and error. ECHO and >> are output redirectors, so you are likely running on a differant shell than you think you are when performing the echo. when you run psexec interactively it shares the invoker shell. just a hunch. – Frank Thomas Jun 17 '15 at 14:27
  • Hi Thomas, I always tried all scenario including yours, but the batch file cannot run. – Nestasoft Jun 17 '15 at 19:35
  • have you permitted the user to login as a batch job? sometimes that is required. you can find the option in secpol.msc -> Local Policies -> security options – Frank Thomas Jun 18 '15 at 11:59
  • I've fixed some problem...and be able to see created file. But PsExec, need the opened session in same machine (which running PsExec), how the application pool can execute command file using PsExec without need that the same application pool opened a session – Nestasoft Jun 18 '15 at 18:36
  • have you tried the -i switch to interact with the server session? – Frank Thomas Jun 18 '15 at 20:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.