CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   how to run .vbs from php (http://www.codingforums.com/showthread.php?t=286738)

Raphael 01-30-2013 02:19 PM

how to run .vbs from php
 
I've spent several hours searching and trying different things, but I can't get this script to run. It is called by a web page:


Code:

error_reporting(E_ALL);
ini_set('display_errors', '1');

if($_POST['store_list'] && $_POST['start_date'] && $_POST['end_date']){
$cmd = 'wscript.exe <path_to_script>/my_script.vbs /store_list:"' . urldecode($_POST['store_list']) . '" /start_date:"' . urldecode($_POST['start_date']) . '" /end_date:"' . urldecode($_POST['end_date']) . '"';

$ws = new COM('WScript.Shell');

$ws->Run('cmd /c ' . $cmd, 0, false);
}




I tried:
  • with & without wscript.exe, cscript.exe, cmd /c, cmd.exe /c, cmd.exe /c /k
  • referencing the path to the .vbs file with /, \, and \\
  • exec()
  • shell_exec()

When I run the script from the command line it works perfectly.

The .vbs file is in a path outside the wwwroot folder. I set the permissions on the .vbs file & the folder it's in to allow read & execute for the Internet Guest Account.

The web page seems to indicate the script is being executed but nothing happens. Process Explorer only shows the error: "[Error opening process]" but no other information.

Please help.

Fou-Lu 01-30-2013 03:09 PM

What is it you're expecting the vbs script to do? Looking at this, there isn't a way to determine what the .vbs has done, so unless you're logging something than it'll be hard to determine what is happening. The COM is a pain since it's marshaled across directly, so you can't do things like error handling using PHP without implementing all the necessary logic.
Not sure what vbs has the capability of doing, but you should start by adding a logging feature. You should be able to flag the run command as well to change the windowed mode (the 0 is hidden), but I'm not 100% sure if it'll remain open. To me it look like it needs a '5' as the option, which MS indicates would do:
Quote:

Activates the window and displays it in its current size and position.
If it stays open and the vbs issues output, I'd expect it to show up in there.

Raphael 01-30-2013 08:43 PM

I made some changes trying psexec, but it still isn't working. However, it runs perfectly from the command line. I think it's gotta be close and it may have something to do with quoting/escaping double quotation marks or /'s or \'s, but I don't know what it's looking for:

PHP Code:

error_reporting(E_ALL);
ini_set('display_errors''1');

if(
$_POST['store_list'] && $_POST['start_date'] && $_POST['end_date']){

    echo 
'processing<br><br>';
    
    
$cmd 'psexec \\<computername.domainname.com> -u <username> -p <password> -e c:\windows\system32\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24"';
    
    echo 
$cmd.'<br><br>';
    
    
$out shell_exec($cmd);
    
    echo 
'[' $out.']<br><br>';
        
    echo 
'<br>end';
    
}
else{
    echo 
'no data';



Fou-Lu 01-30-2013 08:51 PM

Yep, that's always a catcher. I don't see it here, but it doesn't mean it doesn't exist.
I see you printed the $cmd here, so that's a good start. Drag that up from the HTML source itself (not from the browser window), and paste that into the command line. If that's good, we can look at the next step.
Does it ever make it to the echo that indicates the end and the $out, or does it just hang there and timeout eventually?

Old Pedant 01-30-2013 09:03 PM

An alternate suggestion:

Execute a ".bat" file and have that ".bat" file contain the command.

If need be, PHP can create the ".bat" file.

The advantage is that you can actually then LOOK at the ".bat" file to see if it contains what you expect it to contain *and* you can execute it by hand to make sure it works when you do so.

I have even done this when invoking ".vbs" from other ".vbs" code. Just so I can be sure that I'm issuing the command I think I'm issuing.

Raphael 01-30-2013 09:53 PM

Quote:

Originally Posted by Old Pedant (Post 1309855)
An alternate suggestion:

Execute a ".bat" file and have that ".bat" file contain the command.

If need be, PHP can create the ".bat" file.

The advantage is that you can actually then LOOK at the ".bat" file to see if it contains what you expect it to contain *and* you can execute it by hand to make sure it works when you do so.

I have even done this when invoking ".vbs" from other ".vbs" code. Just so I can be sure that I'm issuing the command I think I'm issuing.

That is a great idea. I tried it and it creates the .bat file with no problems. I can run the .bat file from the command line and it works. However, the shell_exec(<filename.bat>) still won't work. I get this error:

CScript Error: Loading your settings failed. (Access is denied. )

Fou-Lu 01-30-2013 10:12 PM

Quote:

Originally Posted by Raphael (Post 1309864)
That is a great idea. I tried it and it creates the .bat file with no problems. I can run the .bat file from the command line and it works. However, the shell_exec(<filename.bat>) still won't work. I get this error:

CScript Error: Loading your settings failed. (Access is denied. )

Sounds like an ownership issue.
One thing to test, is on the command line launch the PHP directly by executing php -f /path/to/your/php/script.php, and seeing what that does. That should use your user credentials to execute the php script which in turn should use the same credentials when launching the shell_exec.
Try that and let us know how it turns out.

I like the bat idea as well, simple and central.

Raphael 01-31-2013 02:51 PM

Quote:

Originally Posted by Fou-Lu (Post 1309867)
Sounds like an ownership issue.
One thing to test, is on the command line launch the PHP directly by executing php -f /path/to/your/php/script.php, and seeing what that does. That should use your user credentials to execute the php script which in turn should use the same credentials when launching the shell_exec.
Try that and let us know how it turns out.

I like the bat idea as well, simple and central.

I put in a
PHP Code:

echo shell_exec('whoami'); 

and it says the script is running under:

nt authority/iusr

I did some research (http://forums.iis.net/t/1147103.aspx) on that and it appears that "nt authority/iusr" replaced the anonymous IIS user in win 2008. Also, the permissions for it aren't configured the same way as a normal user.

I don't know what to do next. Any help will be greatly appreciated!

Fou-Lu 01-31-2013 02:59 PM

Yep, kinda sounded like a privilege issue. I remember how much of a pain it was configuring the 2003 machines with IIS, and I haven't looked back since :P

Take a look at this one: http://www.iis-aid.com/articles/trou...unable_to_fork which indicates to modify the acl for the IIS user. I'm not sure if I like that approach, but I think it beats executing it as another user with more access.

Raphael 01-31-2013 04:12 PM

Quote:

Originally Posted by Fou-Lu (Post 1309999)
Yep, kinda sounded like a privilege issue. I remember how much of a pain it was configuring the 2003 machines with IIS, and I haven't looked back since :P

Take a look at this one: http://www.iis-aid.com/articles/trou...unable_to_fork which indicates to modify the acl for the IIS user. I'm not sure if I like that approach, but I think it beats executing it as another user with more access.

I checked out that site & it was helpful. I tried using icacls to modify the permission on c:\windows\system32\cscript.exe but it kept saying permission was denied. I then copied cscript from \system32 to the same folder where the .vbs file is and I was then able to set the permissions on the copy of cscript to give (RX) access to it.

I also found several pages that said to create these registry keys:
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows Scripting Host
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows Scripting Host\Settings
and set the permissions to allow EVERYONE Read access, which I did.

Doing those things made the
CScript Error: Loading your settings failed. (Access is denied. )
error go away, but the batch file still won't execute.

The batch file now contains a line like this:
Code:

c:\scripts\labor\scheduled\cscript.exe //B c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24"
if I run it from the command line it works perfectly, but the

PHP Code:

shell_exec('c:\scripts\labor\scheduled\repost_labor.bat'); 

still doesn't work. I set the permissions on the .bat, and .vbs files and cscript to allow read & execute access to these accounts:
"nt authority/iusr" (shows up as iusr)
everyone
authenticated users
users

Do you have any more ideas? :)

Raphael 02-01-2013 12:51 PM

So here's where it is now:

I can create the batch file.

The batch file runs perfectly when I run it from the command line.

When I run
PHP Code:

exec('c:\scripts\labor\scheduled\repost_labor.bat'); 

and echo back the output, it just echos the command line inside the batch file but the command doesn't execute. I know this because the .vbs file the batch file is executing logs what it's doing and it doesn't log anything when exec() runs (and it logs everything when I manually run the batch file from the command line).

Why does it just echo back the contents of the batch file without executing it?

Thanks!!


All times are GMT +1. The time now is 06:59 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.