ScorpDevil Posted June 28, 2018 Share Posted June 28, 2018 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted June 28, 2018 Share Posted June 28, 2018 What is the command you have to run manually that works? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 28, 2018 Author Share Posted June 28, 2018 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted June 28, 2018 Share Posted June 28, 2018 Are you saying you don't know what the command to run should be? Or that you know what the command is and don't know how to do it from PHP? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 28, 2018 Author Share Posted June 28, 2018 sorry if I'm not coming across wright. what I'm looking for is how to execute my vbs from php. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 28, 2018 Share Posted June 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 28, 2018 Author Share Posted June 28, 2018 Gotcha! wscript C:\Folder\SubFolder\myscript.vbs "parm1" "parm2" Using the Command Prompt I run the above line Quote Link to comment Share on other sites More sharing options...
requinix Posted June 28, 2018 Share Posted June 28, 2018 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) Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 29, 2018 Author Share Posted June 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 29, 2018 Share Posted June 29, 2018 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? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 29, 2018 Author Share Posted June 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 29, 2018 Share Posted June 29, 2018 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) Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted June 29, 2018 Author Share Posted June 29, 2018 Yes, didn't work. I wish there was a way to check for errors! Quote Link to comment Share on other sites More sharing options...
requinix Posted June 30, 2018 Share Posted June 30, 2018 What is your code now? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted July 2, 2018 Author Share Posted July 2, 2018 $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 Quote Link to comment Share on other sites More sharing options...
requinix Posted July 2, 2018 Share Posted July 2, 2018 Print out the exact value of $cmd and see what happens when you try running it (manually) with that cmd /c. As in C:\Users\You> cmd /C wscript <actual path to myscript.vbs> "<fFile arg>" "<tFile arg>" Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted July 2, 2018 Author Share Posted July 2, 2018 Works! Not problem. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 2, 2018 Share Posted July 2, 2018 Then there's some other problem. What if you set $wait=true? Are you using IIS? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted July 2, 2018 Author Share Posted July 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 2, 2018 Share Posted July 2, 2018 IIS will need permissions to run cmd.exe. But why are you going through WShell in the first place? And then through cmd /c? You know PHP can run commands natively? Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted July 2, 2018 Author Share Posted July 2, 2018 (edited) 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. Edited July 2, 2018 by ScorpDevil Quote Link to comment Share on other sites More sharing options...
requinix Posted July 2, 2018 Share Posted July 2, 2018 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. Quote Link to comment Share on other sites More sharing options...
ScorpDevil Posted July 3, 2018 Author Share Posted July 3, 2018 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.