Solved

How to pass URL parameters to VBS Script File

Posted on 2016-07-18
12
98 Views
Last Modified: 2016-08-01
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?
0
Comment
Question by:jekautz
  • 6
  • 6
12 Comments
 
LVL 15

Expert Comment

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

Open in new window

0
 
LVL 4

Author Comment

by:jekautz
ID: 41719486
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

0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41721999
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.
0
 
LVL 4

Author Comment

by:jekautz
ID: 41727885
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
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41728682
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?
0
 
LVL 4

Author Comment

by:jekautz
ID: 41730265
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41730369
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

0
 
LVL 4

Assisted Solution

by:jekautz
jekautz earned 0 total points
ID: 41732194
I put more effort into that construct, but it never worked.  It seems that Outlook doesn't allow the onclick in <body> or <a> or <p>.  

Here was the output from my code.
<HTML><HEAD><SCRIPT LANGUAGE="VBScript">
Sub LaunchProcess()
Dim Shell
Set Shell = CreateObject("Wscript.Shell")
Shell.Run "cscript c:\temp\myscript.vbs", 1
Set Shell = Nothing
End Sub
</SCRIPT></HEAD><BODY><p onclick="LaunchProcess()">Test.</p><BODY></HTML>

Open in new window

0
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
ID: 41733388
Have you considered instead of using a conventional VBS, publishing an HTA on a webserver, encapsulating your VBS within the HTA, and passing the HTA a value using conventional parameter notation such as:
<a href="http://intranet.company.com/hta/myscript.hta?value=1101">Test with Value parameter set to 1101</a>

Open in new window

?
0
 
LVL 4

Author Comment

by:jekautz
ID: 41733731
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.
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41735418
Some people can think outside of the box.  I often look around and ask, "What box?"
0
 
LVL 4

Author Closing Comment

by:jekautz
ID: 41738269
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now