Link to home
Start Free TrialLog in
Avatar of shacho
shacho

asked on

Passing Parameters to a VBScript From Command Line

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

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

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
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...
Avatar of shacho
shacho

ASKER

What do I need to do inside the script to catch those parameters?  Public variables?
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 shacho

ASKER

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
Avatar of shacho

ASKER

Figured it out.  It's Wscript.Arguments(0).
Avatar of shacho

ASKER

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
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

Avatar of shacho

ASKER

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.

https://www.experts-exchange.com/questions/24311499/Follow-HyperLink-Event-In-Outlook.html

Mike
Avatar of shacho

ASKER

If you're searching the knowledgebase, be sure to scroll to the bottom to see my follow up comments.