raphael75 Posted February 4, 2013 Share Posted February 4, 2013 We have a web portal in a WIMP environment. There is a web page that users can fill out a form. When the form submits I need it to create and execute a batch file on the server that is in a folder outside the wwwroot folder. What happens is the user submits the form & the php is able to create the .bat file in c:/scripts/labor/scheduled/labor_repost.bat, no problem. The contents of the batch file is a single line that looks like: c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24" If I execute the .bat file from the command line manually it works perfectly. The cscript.exe, labor.vbs, and labor_repost.bat files are all in the same folder. In testing, I've tried setting permissions on all these files and the folder they're in to allow these users full control: Everyone Authenticated Users IUSR System Users However, nothing I try from PHP is able to execute the .bat file. I have tried: exec('c:/scripts/labor/scheduled/labor_repost.bat', $out, $retval); $out = shell_exec('c:/scripts/labor/scheduled/labor_repost.bat'); system('c:/scripts/labor/scheduled/labor_repost.bat'); $ws = new COM('WScript.Shell'); $w = $ws->Run('c:/scripts/labor/scheduled/labor_repost.bat', 0, false); I've also tried various other combinations of this by including "psexec" and "cmd /c". None of these will execute the batch file - no matter which one I try, nothing happens. If I echo back the return value from the batch file, it will just echo back the contents of the file. I don't get any error messages. Is it possible that it's a permissions issue? If so, what would need permission to what? We checked the antivirus logs and it doesn't appear to be that. Please help. Thank you! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Tried adding cmd.exe at the start of the execution string for the batch file? Sounds like PHP doesn't invoke the Windows command prompt parser for the bat files, and thus reads them as pure text. Just a guess though, haven't really worked with system execution via PHP on Windows. Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 4, 2013 Author Share Posted February 4, 2013 Tried adding cmd.exe at the start of the execution string for the batch file? Sounds like PHP doesn't invoke the Windows command prompt parser for the bat files, and thus reads them as pure text. Just a guess though, haven't really worked with system execution via PHP on Windows. I tried: cmd.exe cmd /c.exe psexec in combination with all of the different php commands above (exec, shell_exec, etc.). Could it have something to do with the files I'm trying to run not being in the wwwroot folder? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Use cmd.exe /c ...., not what you wrote in the middle. cmd.exe is the name of the command, while /c is a parameter. Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 4, 2013 Author Share Posted February 4, 2013 Use cmd.exe /c ...., not what you wrote in the middle. cmd.exe is the name of the command, while /c is a parameter. That was just a typo. Typing too fast. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Ah, ok. Good to know. Anyway, that should have worked, from what I've been able to find. Though.... I just had a thought, try adding @echo off to the first line of that batch file, and see if that helps. It could be that the file is running, but since batch files automatically echo out the commands they run it just looks like it's returning it as text. Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 5, 2013 Author Share Posted February 5, 2013 (edited) I tried @echo off but it didn't work. Here is the code: $cmd .= 'c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"' . urldecode($store_list) . '" /start_date:"' . urldecode($start_date) . '" /end_date:"' . urldecode($end_date) . '"'; $fname = 'c:\scripts\labor\scheduled\repost_labor.bat'; $fp = fopen($fname, 'w'); fwrite($fp, "@echo off\n" . $cmd); fclose($fp); exec($fname, $out, $retval); print_r($out); echo($retval); $out shows an empty array when I put the @echo off in. If I leave the @echo off out, I get this: Array ( [0] => [1] => C:\inetpub\wwwroot\functions>c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24" ) And $retval echos back 0. Edited February 5, 2013 by raphael75 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 5, 2013 Share Posted February 5, 2013 (edited) OK, then by all indications that batch file is being executed, and it returns with an error level of 0. Try adding "echo Running" between "@echo off" and the command, if you see "Running", and only that, then there is no doubt: The batch file is being executed, and properly so. Your troubles lies elsewhere. Edited February 5, 2013 by Christian F. Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 7, 2013 Author Share Posted February 7, 2013 OK, then by all indications that batch file is being executed, and it returns with an error level of 0. Try adding "echo Running" between "@echo off" and the command, if you see "Running", and only that, then there is no doubt: The batch file is being executed, and properly so. Your troubles lies elsewhere. I tried what you said and "Running" was echo'ed back, so we know the batch file is running. I also again tried adding cmd /c to the batch file, so now the batch file looks like this: @echo off echo Running cmd /c cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-02-03" /end_date:"2013-02-05" What could be blocking the cscript? Could it be a permissions issue? I tried setting the permissions on cscript.exe, labor.vbs, and repost_labor.bat to allow "Everyone", "Users", "Authenticated Users", and "IUSR" to have read/execute or full access. Could antivirus be blocking it? Is there some way I can have the batch file echo back if/why a command doesn't execute (some error code)? Thanks!! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 7, 2013 Share Posted February 7, 2013 These articles should be of use: http://stackoverflow.com/questions/6812484/batch-file-test-error-level http://www.robvanderwoude.com/errorlevel.php http://support.microsoft.com/kb/69576 Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 8, 2013 Author Share Posted February 8, 2013 These articles should be of use: http://stackoverflow...est-error-level http://www.robvander.../errorlevel.php http://support.microsoft.com/kb/69576 I tried adding in the %ERRORLEVEL% after the line where it's supposed to execute the cscript.exe and it echos back 0 ("success"). If there's something that's just blocking PHP executed in the Web context from running .bat or .vbs files in folders other than the wwwroot folder I might have to use a different approach. I think I'll experiment with virtual folders. Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 8, 2013 Author Share Posted February 8, 2013 I figured out a way around the issue. The php web page still generates a .bat file that looks like this: cmd /c cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-02-03" /end_date:"2013-02-05" However, it doesn't try to execute the batch file. Instead, I have this: <?php $cmd = 'schtasks /create /sc once /tn "repost_labor" /tr "c:\scripts\labor\scheduled\repost_labor.bat" /st ' . $time; exec($cmd, $out, $retval); ?> where $time is the next minute after the php script runs. In the .bat file on the line before the cmd /c cscript.exe ... line, the php adds this: cmd /c schtasks /delete /tn "repost_labor" /u <uname> /p <pwd> /f /s <domain name> So even though php can't execute the batch file directly, I can have it create a scheduled task that's set to run 1 minute after the script executes that will then run the batch file. When the scheduled task runs the batch file, the batch file then immediately removes the scheduled task and then runs the cscript. It's not a perfect solution, but it's 99% of the way there. Having it execute 1 minute later is totally fine for what this process needs to do. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 8, 2013 Share Posted February 8, 2013 Probably a "safeguard" that they added to IIS, to prevent Worms from propagating. Seems like you've found the best workaround for it, even though it's somewhat of an hack. Good luck anyway! Quote Link to comment Share on other sites More sharing options...
raphael75 Posted February 8, 2013 Author Share Posted February 8, 2013 Thanks so much for your help! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 8, 2013 Share Posted February 8, 2013 You're welcome, glad I could help. PS: If you've solved this it would be appreciated if you could mark it as such, so that others with the same problem know that they can find useful information in this thread. Quote Link to comment Share on other sites More sharing options...
joanna23 Posted June 20, 2013 Share Posted June 20, 2013 I am having the same problem but unfortunately running the exec($cmd...) to create the schedule task does not work for me either. It behaves the same as when I was trying to exec($cmd...) the .bat file. And of course, running the same $cmd to create the schedule task from the command window works. Running PHP and IIS. Thanks in advanced for any hints, tips as to what I could be missing... Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 20, 2013 Share Posted June 20, 2013 In order to help you we'll have to know what you've tried, which means posting the code you have troubles with. Along with any error messages you're getting (remember to check the error logs), and anything else that you think might be of relevance. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.