Solved

Passing Parameters to a VBScript From Command Line

Posted on 2009-04-12
10
1,028 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
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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

840 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