Solved

Passing Parameters to a VBScript From Command Line

Posted on 2009-04-12
10
1,017 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Outlook creates tmp files 2 44
VB Script: restart service function 30 36
Problem to App source 6 40
Do Not Reply on Exchange 2010 1 12
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

773 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