sanchez77 Posted May 3, 2012 Share Posted May 3, 2012 How do you pass a PHP variable to a VBS function. For example, here the test.vbs file: Function WriteLineToFile(ntext) Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True) f.WriteLine ntext Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading) WriteLineToFile = f.ReadAll End Function WriteLineToFile($arg1) PHP file, test.php $arg1 = 'test 55'; Exec('test.vbs $arg1'); I want to take the variable $arg1 and pass it to the vbs function. Has anyone done something similar or could point me in the right direction? Thanks, sanchez Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/ Share on other sites More sharing options...
batwimp Posted May 3, 2012 Share Posted May 3, 2012 I don't know much about VBS, but for your PHP you need to lower-case your exec function and use double quotes instead: $arg1 = 'test 55'; exec("test.vbs $arg1"); Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342789 Share on other sites More sharing options...
sanchez77 Posted May 3, 2012 Author Share Posted May 3, 2012 Thanks, but the vb script runs with captial Exec and the single quotes. On the vb side it's not putting the value of $arg1, it's putting in $arg1 Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342793 Share on other sites More sharing options...
batwimp Posted May 3, 2012 Share Posted May 3, 2012 You aren't running that section of code from VB, but from PHP, which will not parse variables inside of single quotes (unless those single quotes are inside double quotes). Try changing them to double quotes and you will pass the value of that variable. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342794 Share on other sites More sharing options...
xyph Posted May 3, 2012 Share Posted May 3, 2012 If you must use single quotes you have to concatenate exec('test.vbs '.$arg1); http://php.net/manual/en/language.types.string.php http://php.net/manual/en/language.operators.string.php Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342798 Share on other sites More sharing options...
sanchez77 Posted May 3, 2012 Author Share Posted May 3, 2012 I tried both but it still is just writing arg1 instead of test 55 $arg1 = 'test 55'; //exec("test.vbs $arg1"); exec('test.vbs '.$arg1); Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342810 Share on other sites More sharing options...
batwimp Posted May 3, 2012 Share Posted May 3, 2012 google tells me you're not grabbing the arguments correctly in your VBS. It should be: Function WriteLineToFile(ntext) Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True) f.WriteLine ntext Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading) WriteLineToFile = f.ReadAll End Function Dim Arg, var1 Set Arg = WScript.Arguments var1 = Arg(0) WriteLineToFile(var1) Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342816 Share on other sites More sharing options...
xyph Posted May 3, 2012 Share Posted May 3, 2012 Heh, VB help in a PHP forum. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342817 Share on other sites More sharing options...
sanchez77 Posted May 3, 2012 Author Share Posted May 3, 2012 yay google!!!! Thanks batwimp, i'm new to VB as well, appreciate the insight. In the example below it's dropping the Text part of the variable. Any ideas on that? <?php $arg1 = 'Test Text'; exec("test.vbs $arg1"); ?> I tried both $arg1 ='Test Text'; and $arg1 = "Test Text"; But it still drops the Text part 'Test Text' when writing to the text file. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342819 Share on other sites More sharing options...
batwimp Posted May 3, 2012 Share Posted May 3, 2012 You are essentially issuing this command through the command line by using exec() from PHP. This means that your arguments are separated by a space instead of a comma. So it is treating 'text' as a separate argument instead of being part of the same string. You can set up your VBS to take multiple arguments by adding them in, which should be fairly obvious: NOT TESTED Function WriteLineToFile(ntext,ntext2) Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True) f.WriteLine ntext+ntext2 Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading) WriteLineToFile = f.ReadAll End Function Dim Arg, var1, var1 Set Arg = WScript.Arguments var1 = Arg(0) var2 = Arg(1) WriteLineToFile(var1, var2) But if you want to pass the entire string as a single argument with the space(s) in them, I'm not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342820 Share on other sites More sharing options...
batwimp Posted May 3, 2012 Share Posted May 3, 2012 I found it. In order to pass the variable as a single argument with spaces, you need to enclose it in quotes, which means you need to quote the quotes: $arg1 = '"test 55"'; Note the double quotes are inside the single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342822 Share on other sites More sharing options...
sanchez77 Posted May 3, 2012 Author Share Posted May 3, 2012 check that roger!! That was it, thank you so much. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342823 Share on other sites More sharing options...
trq Posted May 3, 2012 Share Posted May 3, 2012 Thanks batwimp, i'm new to VB as well Why bother then? I don't think VbScript is used anymore by anyone. Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342838 Share on other sites More sharing options...
sanchez77 Posted May 3, 2012 Author Share Posted May 3, 2012 b/c the old database i need to connect/write to is on a Unix Box. The app I'm building is PHP on Windows Box. I need PHP SSH2 functions to connect to it with PHP but b/c windows sucks, I need to be complie it into it's own version to support the SSH functions. I ran into trouble compling it on Win Srv 2008 box and this was the other choice i thought of to use in writing to the the Unix Box. I'm open to suggestion if you have a better idea. PHP Links: http://us3.php.net/manual/en/ssh2.requirements.php http://us3.php.net/manual/en/ssh2.installation.php Building PHP/Windows https://wiki.php.net/internals/windows/stepbystepbuild Other Post regarding compiling on x64 http://www.phpfreaks.com/forums/index.php?topic=358758.0 I'm able to compile on x86, but because I need to use SSH2 and OpenSSL, I need to complie on the x64 box. I know, just use Unix\Linux. If I could I would. Got a better idea? Quote Link to comment https://forums.phpfreaks.com/topic/262029-phpvb/#findComment-1342843 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.