2

I'm try trying to launch remote command via psexec tool, it is running successfully in command prompt. However in c#, I get the following output error:

Access is denied

. here is my command:

psexec \servername -u xxxxxxx -p xxxxxxxxxx -h -w "E:\" cmd /C "(dir)" ^> file.txt

here is my c# source:

ProcessStartInfo PSI = new ProcessStartInfo();
PSI.FileName = "cmd.exe";
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
PSI.CreateNoWindow = true;
PSI.WindowStyle = ProcessWindowStyle.Hidden;
PSI.Arguments = "psexec " + @"\\servername -u xxxxxx -p xxxxxxxxx -h -w "+"\"E:\\\\\" "+"cmd /C " +"\""+"(dir)"+ "\" " +"^> file.txt" ;
p.Start();
p.WaitForExit();
output = p.StandardOutput.ReadToEnd().ToString();
error = p.StandardError.ReadToEnd().ToString();
INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25
Sami Kobbi
  • 309
  • 1
  • 7
  • 17

2 Answers2

0

Sorry, I can't comment yet!

Reading your c# + psexec code, looks like it send psexec \servername -u xxxxxxx -p xxxxxxxxxx -h -w "E:\\" cmd /C "(dir)" ^> file.txt to command prompt

Shouldn't it be "psexec " + @"\\servername -u xxxxxx -p xxxxxxxxx -h -w "+"\"E:\\\" "+"cmd /C " +"\""+"(dir)"+ "\" " +"^> file.txt"; to end with psexec \servername -u xxxxxxx -p xxxxxxxxxx -h -w "E:\" cmd /C "(dir)" ^> file.txt ?

To test I would suggest you to create an string which will receive those values and then show it, just to be sure is the right command being set.

Another thing is PSI.UseShellExecute = false;, I know some command codes must have this set false to work, but I don't know psexec, did you already also tried to set it to true ?

Luis G. Lino
  • 163
  • 12
  • That's not a problem Luis. For the command, the c# string is written correctly. I have already tested it. For UseShellExecute, when I have changed it to true, the program throws this exception: The Process object must have the UseShellExecute property set to false in order to redirect IO streams. – Sami Kobbi Mar 16 '16 at 14:16
0

If the server that runs your code is under a domain, then you'll need to add the domain along with the user name.

Therefore, the -u flag would be -u your_domain_name\username.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Janou
  • 125
  • 1
  • 9