Using Command Line Named Arguments for VbScript

12/07/08

Permalink 09:28:27 am, by dave Email , 552 words, 21360 views   English (US)
Categories: Windows Scripting, Wsh Scripts

Using Command Line Named Arguments for VbScript

Normally when I handle arguments that are passed to a windows script, I use unnamed arguments. The other day, I was looking for something and I can across a reference to named arguments in windows scripting. After a quick look at this, I realized that using named arguments was the way to go.

To get the arguments from the script, I used to use something like this for unnamed arguments:

value = wscript.arguments.item(0)

Now with named arguments, I can use:

value = wscript.arguments.named.item("argument identifier")

[More:]

Named arguments are passed to the script using a forward slash, name or the argument, a colon and then the value, like this:

cscript namedCmdLineArgs.vbs /s:serverName /d:domainName /u:userName /p:userPassword

The commandline shown above is what the code example below is expecting.

Here is a more indepth example of using named arguments.

'----------------------------------------------------------
'- Named Command Line Arguments VBScript Sample
'-
'- Copyright © 2008, Dave Moats
'-
'- This sample is provided 'AS-IS', without any
'- express or implied warranty. In no event will the
'- authors be held liable for any damages arising from
'- the use of this sample code.
'-
'- Permission is granted to anyone to use this sample
'- code for any purpose, including commercial applications,
'- subject to the following restrictions:
'-
'- The origin of this code must not be misrepresented;
'- you must not claim that you wrote the original code.
'-
'- If you use this code, an acknowledgment in the
'- documentation is requested - shown below:
'-
'- Portions Copyright © 2008,
'- Dave Moats (http://www.davemoats.com/).
'-
'----------------------------------------------------------

'----------------------------------------------------------
'- NOTE: watch for wrapped lines and html special
'- characters in the web representation of this
'- sample code
'----------------------------------------------------------

'----------------------------------------------------------
'--
'-- namedCmdLineArgs.vbs - example of how to pass named
'-- arguments to a script
'--
'----------------------------------------------------------
option explicit

'----------------------------------------------------------
'-- declare the local variables to be used
'----------------------------------------------------------
dim unamedArgs, namedArgs, scriptName
dim serverName, domainName, userName, userPswd

'----------------------------------------------------------
'-- get the name of the running script
'----------------------------------------------------------
scriptName = wscript.scriptname

'----------------------------------------------------------
'-- get the named arguments
'----------------------------------------------------------
set namedArgs = wscript.arguments.named

'----------------------------------------------------------
'-- check to see if all the required arguments were passed
'----------------------------------------------------------
if not namedArgs.exists("s") then
   wscript.echo "Usage: " & scriptName & " /s:<server name> is required"
   wscript.quit
else
   serverName = namedArgs.item("s")
end if

if not namedArgs.exists("d") then
   wscript.echo "Usage: " & scriptName & " /d:<domain name> is required"
   wscript.quit
else
   domainName = namedArgs.item("d")
end if

if not namedArgs.exists("u") then
   wscript.echo "Usage: " & scriptName & " /d:<user name> is required"
   wscript.quit
else
   userName = namedArgs.item("u")
end if

if not namedArgs.exists("p") then
   wscript.echo "Usage: " & scriptName & " /p:<user password> is required"
   wscript.quit
else
   userPswd = namedArgs.item("p")
end if

'----------------------------------------------------------
'-- echo out the arguments
'----------------------------------------------------------
wscript.echo "Server: " & serverName & vbcrlf & "Domain: " & domainName & vbcrlf & "User: " & userName & vbcrlf & "Password: " & userPswd

Pay close attention to the lines wrapping in this sample, the script does not have any multi-line statements.

I hope this post helped out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.

Dave



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

This post has 31 feedbacks awaiting moderation...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 3

powered by b2evolution free blog software