Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing Parameters to a VBScript From Command Line

Posted on 2009-04-12
10
Medium Priority
?
1,105 Views
Last Modified: 2013-11-05
Is there a way to pass parameters to a vbscript from the command line?
Basically, what I'm trying to do is something like this:
C:\> C:\MyScripts\OpenHTM.vbs "http:\\www.mysite.com"

More specifically, I want to configure this shell handler to open my vbscript and pass the URL, rather than open a specific browser.
Key: HKEY_Local_Machine\Software\Classes\htmlfile\shell\open\command
Current Value: "C:\Program Files\Internet Explorer\iexplore.exe" -nohome

Mike

0
Comment
Question by:shacho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 1040 total points
ID: 24127425
Yes....something like this:

myURL = Wscript.Arguments(0)

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24127429
One more thing - the default engine is wscript, which is what's used when you double-click the script.
Use

cscript openhtm.vbs http:\\www.mysite.com

to run this...
0
 

Author Comment

by:shacho
ID: 24127444
What do I need to do inside the script to catch those parameters?  Public variables?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 960 total points
ID: 24127515
Hello Mike,

From the command line, the syntax is...

c:\folder\subfolder\MyScript.vbs /Company:"Foo" /StartDate:"15 Jul 2008" /EndDate:"15 Jul 2009"

You can then run some validation for the command line arguments.



Escape = False
Set NamedArguments = WScript.Arguments.Named

If Not NamedArguments.Exists("Company") Then Escape = True

If Not NamedArguments.Exists("StartDate") Then
    StartDate = DateAdd("y", -1, Date)
ElseIf Not IsDate(NamedArguments("StartDate")) Then
    Escape = True
End If

If Not NamedArguments.Exists("EndDate") Then
    EndDate = DateAdd("y", -1, Date)
ElseIf Not IsDate(NamedArguments("EndDate")) Then
    Escape = True
End If



So now, after that, if the variable Escape = True then you kill the script.

Regards,

Patrick
0
 

Author Comment

by:shacho
ID: 24127572
Groovy.  One problem.  I'm not sure how to get a hold of the parameter itself.  If I click on a link, currently that link is passed by HKEY_Local_Machine\Software\Classes\htmlfile\shell\open\command to IE by way of "C:\Program Files\Internet Explorer\iexplore.exe" -nohome.  Notice that there is nothing in that command that exposes the URL that was passed.  Is there a WSript object separate from NamedArguements that could catch parameter?

Mike
0
 

Author Comment

by:shacho
ID: 24127583
Figured it out.  It's Wscript.Arguments(0).
0
 

Author Comment

by:shacho
ID: 24128194
Almost there!  Here's my script:

url = wscript.arguments(0)
if instr(1, url, "acmecompany") then
  set ieapp = createobject("internetexplorer.application")
  ieapp.navigate url
  ieapp.visible = true
  set ieapp = nothing
else
  set shl = wscript.createobject("wscript.shell")
    shl.run """c:\program files\mozilla firefox\firefox.exe""" & url
  set shl = nothing
end if

Last thing - is there a way to run this script without displaying
a shell window during execution (no echo)?

Mike
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24128966
Drop a comma 0 at the end:
url = wscript.arguments(0)
if instr(1, url, "acmecompany") then 
  set ieapp = createobject("internetexplorer.application")
  ieapp.navigate url
  ieapp.visible = true
  set ieapp = nothing
else
  set shl = wscript.createobject("wscript.shell")
    shl.run """c:\program files\mozilla firefox\firefox.exe""" & url ,0 '<<<<here
  set shl = nothing
end if

Open in new window

0
 

Author Comment

by:shacho
ID: 24134137
Just tried it, but the window still displays.  It also displays for IE links, so I don't think it's the shell command that's causing it.  I am ultimately trying to have Windows pass all clicked URLs into this script for handling, rather than passing them directly to a browser, which is what it normally does.  I have configured the file type OPEN action for URLs as "c:\windows\system32\cscript.exe" "c:\shell\openurl.vbs"  %1.  I'm pretty sure that's what's causing it to pop up.

...

Flash forward - just figured out that if I change "csript" to "wscript" I can prevent the console window from showing.

"c:\windows\system32\wscript.exe" "c:\shell\openurl.vbs"  %1

Works like a charm.  

...

I have been pursuing this project with a different approach, and in fact that is the solution I went with.  If you're interested in seeing what I did, here's the link.

http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_24311499.html

Mike
0
 

Author Closing Comment

by:shacho
ID: 31569386
If you're searching the knowledgebase, be sure to scroll to the bottom to see my follow up comments.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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