Solved

Passing Parameters to a VBScript From Command Line

Posted on 2009-04-12
10
988 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
  • 6
  • 3
10 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 260 total points
Comment Utility
Yes....something like this:

myURL = Wscript.Arguments(0)

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
What do I need to do inside the script to catch those parameters?  Public variables?
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 240 total points
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:shacho
Comment Utility
Figured it out.  It's Wscript.Arguments(0).
0
 

Author Comment

by:shacho
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you're searching the knowledgebase, be sure to scroll to the bottom to see my follow up comments.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

763 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

7 Experts available now in Live!

Get 1:1 Help Now