Valid Case for Arguments
Use arguments to dictate variables when calling a script.
Establishing variables for certain things just makes good sense. If there is a path to a file or directory, that's a perfect candidate for a variable. That way, the code can be easily changed in the future or can be reused in another script. Sometimes you may even set a variable to easily change the behavior of your script, such as if your script should display information or not, if it should take actions or just display what it would do for testing purposes. I'm sure you can think of many other such "yes or no" type options you may want to control when you run the script.
If you want to change these values frequently or if you'll be sharing this script for others to execute, constantly editing these values in the code may not be desirable. This is where arguments become a valuable feature.
By handling arguments, your script can function much as a command line tool. Whatever options your script provides may be handled by these parameters as you dictate in your code.
The Windows Script host supports arguments as property values where you may check how many arguments have been passed to your script and what text each contains. A space is the delimiter that separates parameters, not the common dash or slash characters used to identify command line parameters (you can require or simply ignore such identifiers if you wish). How you want to handle arguments will depend very much on your script and what it does.
If you make use of arguments today you know how valuable they can be. If you've never worked with arguments before, here's a skeleton that's aimed at showing you how to easily take advantage of them in your scripts:
If WScript.arguments.count = 0 Then
For i = 0 To (WScript.arguments.count - 1)
sCurrentArgument = WScript.Arguments.Item(i)
Select Case (UCase(sCurrentArgument)
Case "/A", "-A"
optionA = True
Case "/B", "-B"
optionB = True
Case "/C", "-C"
optionC = True
WScript.Echo "Invalid Argument Passed: " &_
sCurrentArgument & ". Aborting."
If optionA Then WScript.Echo "Do actions for option A." End If
If optionB Then WScript.Echo "Do actions for option B." End If
If optionC Then WScript.Echo "Do actions for option C." End If
Tech Help—Just An
Got a Windows, Exchange or virtualization question
or need troubleshooting help? Or maybe you want a better
explanation than provided in the manuals? Describe
your dilemma in an e-mail to the MCPmag.com editors
the best questions get answered in this column and garner
the questioner with a nifty Redmond T-shirt.
When you send your questions, please include your
full first and last name, location, certifications (if
any) with your message. (If you prefer to remain anonymous,
specify this in your message, but submit the requested
information for verification purposes.)
This script lets you know if an A, B or C switch is passed upon execution. Using this same code and logic, you should be able to easily implement command line arguments in your scripts. Try it out!
Bob Kelly is president and co-founder of AdminScriptEditor.com, home to an integrated suite of scripting tools and a shared library of scripts and language help. He has authored books on scripting and desktop administration and several white papers. Bob also owns and operates AppDeploy.com, where he writes and produces videos on topics related to software deployment.