Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2013, 02:19 PM   PM User | #1
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
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.
Raphael is offline   Reply With Quote
Old 01-30-2013, 03:09 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 01-30-2013, 08:43 PM   PM User | #3
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
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';

Raphael is offline   Reply With Quote
Old 01-30-2013, 08:51 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 01-30-2013, 09:03 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-30-2013, 09:53 PM   PM User | #6
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
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. )
Raphael is offline   Reply With Quote
Old 01-30-2013, 10:12 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Raphael View Post
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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 01-31-2013, 02:51 PM   PM User | #8
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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!
Raphael is offline   Reply With Quote
Old 01-31-2013, 02:59 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 01-31-2013, 04:12 PM   PM User | #10
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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 is offline   Reply With Quote
Old 02-01-2013, 12:51 PM   PM User | #11
Raphael
New Coder

 
Join Date: Mar 2009
Posts: 70
Thanks: 3
Thanked 3 Times in 3 Posts
Raphael is an unknown quantity at this point
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!!
Raphael is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:27 PM.


Advertisement
Log in to turn off these ads.