Jump to content

Run WScript from PHP


ScorpDevil

Recommended Posts

Hi Guys -

I'm running a php script as follow:

   $obj = NEW COM('WScript.Shell');
    //
    if (is_object($obj)) {
        //
        $cmd = 'wscript "C:\dir\subdir\move_file.vbs"';
        //
        $obj->Run('cmd /C '.$cmd, 0, $wait);
        //
    } else {
        //
        echo 'can not create wshell object';
        //
    }
    //
    $obj = null;
    //

The vbs simply creates a copy of the a file in another location.

Works perfectly when I hard code the source and destination inside the vbs.
But I can't make it work passing the source and destination as parameters

Bottom line, How to build my $cmd with the parameters?!

I tried the following but no luck

    $cmd = 'wscript ""C:\dir\subdir\move_file.vbs ';
    $cmd = $cmd."/From:".Chr(34).Chr(34)."$fFile".Chr(34).Chr(34)." ";
    $cmd = $cmd."/To:".Chr(34).Chr(34)."$tFile".Chr(34).Chr(34).Chr(34).Chr(34);

I'd like to add that I'm totally new to php.

Thanks a lot

SD

Link to comment
Share on other sites

Hi requinix -

I ran the following:

$cmd = 'wscript "C:\_myassistant$\scripts\move_file.vbs"';

Then I add the raw location and destination inside my script and worked.

I replaced the following variables

	'--#
	fFile = arg.Item("From")
	tFolder = arg.Item("To")
	'--#

With the following:

	'--#
	fFile = "C:\Folder\Subfolder\document.pdf"
	tFolder = "C:\AnotherFolder\AnotherSubfolder\"
	'--#

Here is my script:

Option Explicit
'--#
Dim fso
Dim fFile
Dim tFolder
Dim arg
'--# 
Set arg = WScript.Arguments.Named
'--#
If (arg.Exists("From")) Then
	'--#
	fFile = arg.Item("From")
	tFolder = arg.Item("To")
	'--#
	Set fso = CreateObject("Scripting.FileSystemObject")
	'--#
	If (fso.FileExists(fFile)) Then
		'--#
		If (fso.FolderExists(tFolder)) Then
			'--#
			fso.CopyFile fFile, tFolder
			'--#
			WScript.Echo "Done"
			'--#
		Else
			'--#
			WScript.Echo "Folder Does Not Exists"
			'--#
		ENd If
		'--#
	Else
		'--#
		WScript.Echo fFile & " Does Not Exists"
		'--#
	End If
	'--#
	Set fso = Nothing
	'--#
Else
	WScript.Echo "Missing parameters"
End If

Then I navigate to my php page and the file is copied.

The question is, How to pass the values needed as parameters

Thanks

Link to comment
Share on other sites

Yes. But what I'm trying to figure out first is how you need to run the script without PHP. As in what do you have to type at the command line for it to run correctly? When you have that, then we can figure out how to get it into PHP.

Link to comment
Share on other sites

Then in PHP the command starts looking like

'wscript C:\Folder\SubFolder\myscript.vbs "parm1" "parm2"'

If you have variables $parm1 and $parm2 then

'wscript C:\Folder\SubFolder\myscript.vbs "$parm1" "$parm2"'

And you need to escape the variables using escapeshellarg, which includes the quotes too, so

'wscript C:\Folder\SubFolder\myscript.vbs ' . escapeshellarg($parm1) . ' ' . escapeshellarg($parm2)

 

Link to comment
Share on other sites

I thank you for teaching me about escapeshellarg(), but still not working.

I can't remember where I read about enclosing the WScrip in between quotes?

wscript "C:\myfolder\subfolder\myscript.vbs ""parm1"" ""parm2"""

But no luck either.

And also I get no error. Hard to understand what's really happening.

Link to comment
Share on other sites

So does that mean when I asked you what command to run and you said

5 hours ago, ScorpDevil said:

Gotcha!


wscript C:\Folder\SubFolder\myscript.vbs "parm1" "parm2"

Using the Command Prompt I run the above line

you were... what? Making it up? Lying?

 

One more time. What is the EXACT command you have to run to make your script work?

Link to comment
Share on other sites

I don't understand you. That is the correct command when executed via command prompt. What I said was:

I was reading somewhere about using quotes when running WScript from PHP COM() class.

I'm assuming the reasons to close the command string in between quotes is because of the blank spaces in between parameters and actual script, but I could be wrong. No reasons to lie or making things up.

That's the reason I'm here asking you questions on how to use the COM() class.

Link to comment
Share on other sites

Then maybe I misunderstood what you were trying to do by talking about a new command with more quotes. So I'll just forget you said it.

Did you try what I said earlier?

19 hours ago, requinix said:

And you need to escape the variables using escapeshellarg, which includes the quotes too, so


'wscript C:\Folder\SubFolder\myscript.vbs ' . escapeshellarg($parm1) . ' ' . escapeshellarg($parm2)

 

 

Link to comment
Share on other sites

	$wait = false;
	//
	$obj = NEW COM('WScript.Shell');
	//
	if (is_object($obj)) {
		//
		$cmd = 'wscript C:/path/to/myscript.vbs ';
		$cmd = $cmd.escapeshellarg($fFile).' ';
		$cmd = $cmd.escapeshellarg($tFile);
		//
		$obj->Run('cmd /C '.$cmd, 0, $wait);
		//
	} else {
		//
		echo 'can not create wshell object';
		//
	}
	//
	$obj = null;
	//

Here you go. I'll continue to try new stuff and see what comes up. Thanks a lot for the help

Link to comment
Share on other sites

I actually started to noticed that. I looked at the Task Manager of my 2012 srvr and I can see the request coming thru but nothing happens. I'm wondering if this has something to do with permissions. I running IIS 8.

I did try setting the wait to true and I just see the spinning circle going and going.

Services.PNG

Link to comment
Share on other sites

I'm all open to suggestions. I noticed functions like shell_exec(), exec() and system() and nothing seems to work or perhaps I'm doing something wrong (most likely that).

The main reason to use vbs scripts was to avoid giving write permissions to IIS Users. So, I was thinking of giving my internal script the permissions to do so and not php/IIS users.

Link to comment
Share on other sites

That might work, I don't know (but I'm doubtful), however to launch it at all IUSR would need permissions on cmd.exe. The shell. Because that's what WScript.Shell.Run and shell_exec/exec/system all need.

But you can use popen instead. I think. I don't believe it uses a shell, but if so then you'll need the full proc_open. Still, IUSR will need permissions on wscript.exe though. Can't get around that: must have permissions on the thing being launched, whether that's cmd.exe or wscript.exe or whatever.

So either you grant privileges to run cmd.exe and use one of the exec functions, or you grant privileges to run wscript.exe and use popen/proc_open. COM is definitely not the way to go.

Link to comment
Share on other sites

requinix -

First of all let apologies for wasting your time. The code worked using COM(). The problem was my absolute path the my internal script.

	$wait = false;
	//
	$obj = NEW COM('WScript.Shell');
	//
	if (is_object($obj)) {
		//
		$cmd = 'wscript C:\scripts\copyfile.vbs ';
		$cmd = $cmd.escapeshellarg($fFile).' ';
		$cmd = $cmd.escapeshellarg($tFile);
		//
		$obj->Run('cmd /C '.$cmd, 0, $wait);
		//
		echo 'OK';
		//
	} else {
		//
		echo 'ERROR';
		//
	}
	//
	$obj = null;

Thank you very much. works with no problem. hope this helps another php enthusiast

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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