2

What I am trying to do is run a batch file that is located on the webserver's desktop when a button is clicked. This is what I have thus far.

ProcessStartInfo psi = new ProcessStartInfo("Notepad.exe");
   psi.WorkingDirectory = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories";
   psi.UseShellExecute = false;
   psi.UserName = "administrator";  //username
   psi.Password = secure;  //secure string
var process = Process.Start(psi);

When I debug it, it does exactly what I want it to, but when I access the site on IIS (localhost:81) the batch file never runs. I've tried many different variants of ProcessStartInfo and Process with no luck. I've tested the username and password and they are both correct too.

I don't get any errors, the button just triggers a page post back.

I have given IUSR and IIS_IUSRS permissions to the file, and still nothing runs. I also removed the username and password and set the UseShellExecute to true, but that did nothing as well.

EDIT:

It looks like everyone thinks its some permissions. Any idea on what I need to do to allow IIS to open the process?

Thanks in advance!

Prock
  • 410
  • 1
  • 5
  • 20
  • it would be running under the context of the user account that is running the website (AppPool).... – Ahmed ilyas Jul 17 '14 at 14:06
  • This really sounds like permissions on IIS. Try to impersionate a user instead, something similar. This is all kinda new to me aswell – Bruno Monteiro Jul 17 '14 at 14:07
  • try: psi.Verb = "runas" and also psi.ShellExecute=true – Robert Jul 17 '14 at 14:15
  • I agree with the others, it sounds very much like a permissions issue. – bowlturner Jul 17 '14 at 14:20
  • @Prokzy: In a comment below you said: "nothing pops up on the server". What's in your batch file? Because you shouldn't see a window open .. – NotMe Jul 18 '14 at 16:44
  • @ChrisLively Now instead of testing on the batch file, I am trying to open notepad. As soon as I get something to work I'll make it open the script it needs to. – Prock Jul 18 '14 at 16:53
  • That will never work. IIS can't interact with the desktop. – NotMe Jul 18 '14 at 16:56
  • Is there no workaround to this? – Prock Jul 18 '14 at 16:58
  • See this: http://forums.asp.net/t/1757464.aspx?how+to+give+permission+for+iis+user+for+execute+cmd However, IIS wasn't built to support this type of thing and they even went out of their way to make it really really hard. I'd suggest you go some other route. – NotMe Jul 18 '14 at 17:00
  • Thanks for the input. I'll probably look into doing this project another way. I appreciate all the help! – Prock Jul 18 '14 at 18:22

1 Answers1

0

Just change the third line to this:

  psi.UseShellExecute = true;

or read this article which really gets the work done:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;889251

chemark
  • 1,181
  • 1
  • 13
  • 19
  • 1
    Are you sure? Using the Shell on a web server sounds like a veeeeeeeeeeeeery strange idea. – Uwe Keim Jul 17 '14 at 14:18
  • @UweKeim but so does running a batch file on the desktop of the administrator user account, so... ;-) – slippyr4 Jul 17 '14 at 14:20
  • UweKeim this risk exists from the start as @slippyr4 says! Just read this http://stackoverflow.com/questions/5255086/when-do-we-need-to-set-useshellexecute-to-true – chemark Jul 17 '14 at 14:28
  • 1
    IMHO, a desktop vs publicly(?) accessible, though OP didn't really say, web server...Big difference in strange factor :) – EdSF Jul 17 '14 at 14:51
  • When I try and run with the suggested answer, it still does the same thing. Nothing pops up on the server. I'm guessing its some permissions, but I'm not sure exactly what it is. – Prock Jul 17 '14 at 15:42