Jump to content

can't execute .bat file from php


raphael75

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by raphael75
Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 months later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.