Link to home
Start Free TrialLog in
Avatar of jekautz
jekautz

asked on

How to pass URL parameters to VBS Script File

I have a VBS Script that accepts parameters from the command line.  For example, the script will retrieve record 1011.  This works on the command line.:
c:\temp\myscript.vbs 1011

Open in new window

Now to take it a step further, my program sends an email to a user with a hyperlink which will run the script, but when I try to pass a record number in the hyperlink, I get nothing no parameters.

This will work from the Run command: "file:///c:\temp\myscript.vbs" 1011
I have tried variations of the hyperlink with no success:
<a href='"file:///c:\temp\myscript.vbs" 1011'>Test</a>
<a href='"file:///c:/temp/myscript.vbs" 1011'>Test</a>
<a href="'file:///c:\temp\myscript.vbs' 1011">Test</a>
<a href='%22file:///c:\temp\myscript.vbs%22%201011'>Test</a>

Any suggestions?
Avatar of Darrell Porter
Darrell Porter
Flag of United States of America image

Have you tried
<a href='file:///c:\temp\myscript.vbs?1011'>Test</a>

Open in new window

Avatar of jekautz
jekautz

ASKER

I passed this into the email and it opens the script, but I get no parameter.
<a href='file:///c:\temp\myscript.vbs?1011'>Test</a>

Open in new window

Here is my VBS code that retrieves the parameters.  The two msgbox lines return 0 records and no recNum.
Sub getParam(recNum)
	' Store the arguments in a variable:
	Set objArgs = Wscript.Arguments
	wscript.echo objargs.count	
	
	If objArgs.Count > 0 Then
		recNum = objArgs(0)
	End If
        msgbox objargs.count
        msgbox recNum
End Sub

Open in new window

Okay, fine!

If you're willing, try this:

<a href="#" onclick='new ActiveXObject("wscript.shell")_
          .Run("\"\\\\server\\directory\\scriptname.vbs\" Parameter")'>JScript</a>
<br>
<a href="#" onclick='vbscript:createobject("wscript.shell")_
                    .Run """\\server\directory\scriptname.vbs"" Parameter"'>VBScript</a>

Open in new window


And, of course, replace Parameter with your parameter of 1011.
Avatar of jekautz

ASKER

Sorry, but this didn't work either.  I found this post that explains everything I have gone through and will likely follow his lead on a Plan B solution.
after not getting any suitable responses and after continuing my research for this without success, I will temporarily conclude that what I am looking for is not possible. If the reasons for this not being possible concern security, then I am completely puzzled as to why running an exe without parameters is allowed and therefore considered LESS harmful than running an exe with parameters. Surely, an exe does not require parameters to be harmful. If anyone would care to elaborate on this, it would be educational for me, thank you. Nevertheless, I have thought of an alternative to my problem, which is not as elegant as would otherwise be stating parameters in href, but it serves my purpose. Because our app is run from a network drive, I have changed it so that it creates user-specific cmd files on the network drive and the hyperlink in the email it creates points to these cmd instead of the exe. That way, the cmd files contains the call to the exe with the correct parameters and as the cmd files are very light and quickly written, this is OK. I am closing this as "answered", but it is not. Thanks for taking your time to read.

Leo
stackoverflow.com
Instead of an HTTP or HTTPS URI, have you tried this construct:

vbsfile://c:\PathToScript\scriptname.vbs%20%22Parameter%22

?

Look in the registry of one of your workstations in HKey Classes Root and search for VBSFILE
Note the Shell\Open\Command Default value
Maybe this is the right direction?
Avatar of jekautz

ASKER

The Shell\Open\Command is %SystemRoot%\System32\WScript.exe "%1" %*

I can get the hyperlink to successfully run wscript.exe and even myscript.vbs, but it all fails when I tried to add a parameter.  If I were to use wscript.exe then I would need tell it which script to open--which would be another parameter.  I could not get that to work either.

I have tried many variations of
"script.vbs" param
"script.vbs" "param"
script.vbs "param"
wscript.exe script.vbs param

I've tried double and single quotes and even tried replacing special characters with their URL Encoding Reference counterparts.  In all cases, when a parameter is added one of two things happen.  1) Nothing. I click the link and nothing happens.  2) a Windows dialog says it cannot find the file "c:\folder\script.vbs%20parameter". The parameter is always passed as part of the filename.

Trying vbsfile://c:\script.vbs was a good idea, but it would ask which program I want to open it with and even though I would choose the Windows Scripting Host program, it never led anywhere.
In the latest variant, did you try placing the question mark between the script name and the parameter?

Have you tried this sort of construct?

<HTML>
  <HEAD>
    <SCRIPT LANGUAGE="VBScript">
      Sub LaunchProcess()
        Dim Shell
        Set Shell = CreateObject("Wscript.Shell")
        Shell.Run "cscript c:\scriptname.vbs Parameter1 Parameter2",1
        Set Shell = Nothing
      End Sub
    </SCRIPT>
  </HEAD>
  <BODY ONLOAD=LaunchProcess()>
  <BODY>
</HTML>

Open in new window

SOLUTION
Avatar of jekautz
jekautz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jekautz

ASKER

You are full of good ideas, and I think this one has the best chance of working.  However, I won't be able to get to this until tomorrow or Monday.  But I will post my results here.
Some people can think outside of the box.  I often look around and ask, "What box?"
Avatar of jekautz

ASKER

Solution!  Thank you for sticking around WalkaboutTigger.

With WalkaboutTigger's examples, you can email a hyperlink pointing to http://server/some.hta?value=parameter1 which can run a shell to open an executable with parameters.  The only downside to this solution is that browsers prefer to download the HTA as a file.  The extra steps of downloading/locating/running the HTA with additional security dialogs could disconnect some users, but this is a workable solution to the problem.