0

I'm trying to run a .bat file using PHP from the command-line. I'm using Windows Vista Home Premium.

When I use the script on a file like ipconfig.exe, I get the output. However, when I run the .bat file, it gives me output of what is in the file but it doesn't execute it.

What is below works and give me output:

$runCommand = "C:\\WINDOWS\\system32\\ipconfig.exe";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

But this doesn't:

$runCommand = "C:\\Temp\\foo.bat";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
echo "<p>$output</p>";

Here's what's in my foo.bat file:

C:/windows/system32/schtasks.exe /create /tn "TestTask" /tr "C:/Temp/configure.php" /sc minute /st 08:00:00

If I copy this and paste in my Windows Command Line, this command executes successfully.

Not sure what is going on. Kindly assist.

RobertPitt
  • 54,473
  • 20
  • 110
  • 156
BannerMan
  • 10,370
  • 18
  • 73
  • 121

1 Answers1

1

That's because a bat file is a queued list of commands for a prompt. Try the following:

cmd /c myfile.bat

(it may be /k too, forget which executes and closes)

Also, duplicate of How do you run a .bat file from PHP?

EDIT

<?php
  // http://www.php.net/manual/en/function.exec.php#85930

  $_ = null;

  // If you care about the return value, use this:
    passthru("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat",$_);
    header('Content-Type: text/plain');
    echo $_;
  // if you don't care, just use this:
    $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c custom.bat");
?>
Community
  • 1
  • 1
Brad Christie
  • 96,086
  • 15
  • 143
  • 191
  • I replaced C:\\Temp\\foo.bat with C:\\WINDOWS\\system32\\cmd.exe /k C:\\Temp\\foo.bat but it still doesn't execute. – BannerMan Dec 07 '10 at 18:35
  • Please see the link I mentioned in my answer that points tot he other question relating to batch files and php. They have a wonderful example in the accepted answer. – Brad Christie Dec 07 '10 at 18:38
  • I tried exec('C:\WINDOWS\system32\cmd.exe /c START C:\Temp\foo.bat', $out), but it crashed my Apache server. Not sure how you got that to work?! – BannerMan Dec 07 '10 at 20:00
  • 1
    Please see my updated answer. Also, be sure to mark it as accepted since I've googling and RTM'n for you. – Brad Christie Dec 07 '10 at 20:12
  • Tried this: $_ = null; passthru("C:\\WINDOWS\\system32\\cmd.exe /c C:\\Temp\\foo.bat",$_); header('Content-Type: text/plain'); echo $_; Still not working. – BannerMan Dec 07 '10 at 20:31
  • Also tried: $_ = exec("C:\\WINDOWS\\system32\\cmd.exe /c C:\\Temp\\cronwin.bat"); Doesn't work either. I wonder if there was a way to tweak my initial script, at least with that I did get some output, but maybe I missed something in the declaration. – BannerMan Dec 07 '10 at 20:35
  • Then you're probably running in safe mode or something is off on your configuration. what does `echo php_get('save_mode');` return? Could also be listed in `echo ini_get('disable_functions');` – Brad Christie Dec 07 '10 at 20:39
  • php_get gives me a 'Call to undefined function error', ini_get('disable_functions') gives me nothing. – BannerMan Dec 07 '10 at 21:07
  • Sorry, ini_get in both scenarios. – Brad Christie Dec 07 '10 at 21:11
  • No output to the screen with ini_get('save_mode') – BannerMan Dec 07 '10 at 21:42
  • Can definitely tell it's getting late in the say, `ini_get('safe_mode');` On both accounts, my mistake. If that doesn't work I'm out of solutions. Something is messed up and I'm not sure what. All I can recommend is try going to your php directory and finding `php.ini-recommended`, copy it, and rename the copy to php.ini (all in same folder) then try again. If that doesn't work, exhaust google. – Brad Christie Dec 07 '10 at 21:53
  • I figured, I should have noticed that myself. My php.ini file has safe_mode set as Off. Not sure what might be causing this. Could it be my Windows version?! – BannerMan Dec 08 '10 at 01:00
  • Unless it has something to do with the UAC disabling PHP from running the script (possible). But your web service should be running at elevated privileges (and I assume the execution is spawned off the service, thus inheriting the same authorities). Do a search against PHP Windows 7 and exec/passthhru. – Brad Christie Dec 08 '10 at 01:13
  • Ok. I get output when I do something simple like this: exec('C:\\WINDOWS\\system32\\ipconfig.exe', $out); print_r($out); So I figure exec() is working. It just seems like I'm missing something in the syntax. Anyway, thanks, I'll keep looking for a solution. – BannerMan Dec 08 '10 at 12:11