Article

Supporting optional arguments

Christa Anderson

The named arguments collection has a handy feature by which you can check to see if a particular type of value was entered at all. For example, say you'd like to run a script for a particular user account, but you want to default to the currently logged-in user if no name is provided. You also want the script to let the person running it know which name it's running under -- or that it's running for them if no name was provided. While you could use the Wscript.Network object to return the name of the currently logged-in user, an inattentive administrator might not notice that the script is running on themselves or might not be sure why the script is behaving that way.

So, we'll write the script to check for the existence of a "name" argument. If it exists, the script will report that the script is running for that person. If it does not, the script will report that the script is running for the current user.

Set colNamedArgs = Wscript.Arguments.Named
Select Case colNamedArgs.Exists("name")
Case True Wscript.Echo "This script applies to", colNamedArgs("name")
Case Else Wscript.Echo "Because you did not supply a name, the script will run for the current user."
End Select

To support this, we'll use the Named collection's Exists method. Exists can return a value of True or False.

Scripting School: Windows Script Host arguments

    Requires Free Membership to View

Read Christa's previous columns:
-Beginner's guide to scripting
-It's time to increase your scripting expertise
-Scripting: Connect users to network resources
-Scripting School: More on connecting to network resources
-Scripting School: Find objects with Windows Scripting Host

ABOUT THE AUTHOR:
When Christa Anderson began working with Windows Server operating systems in 1992, she became increasingly interested in finding more efficient and flexible ways of performing routine tasks. Christa has written extensively about administrative scripting and taught technical sessions on the subject at conferences such as Comdex and CeBIT, helping people who had never done any scripting to write their own scripts in half a day. In addition to her interest in scripting Windows management, Christa is an authority on server-based computing and the program manager for Terminal Services licensing in Longhorn. If you have a scripting question for Christa, please e-mail her at scripting@SearchWinSystems.com.


There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: