Solved

Passing Parameters to a VBScript From Command Line

Posted on 2009-04-12
10
1,068 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 260 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
Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 240 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
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…

688 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