Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

November 14, 2005 12:00 AM

Function Determines the Script Host On the Fly

Windows IT Pro
InstantDoc ID #48246
Rating: (0)
Downloads
48246.zip

One of the most common tasks that I need to perform in Windows Script Host (WSH) scripts is to determine the script host (i.e., cscript.exe or wscript.exe) that's executing the current script. Determining the script host is important when, for example, a script uses the WScript object's Echo method repeatedly to display output. You don't want to use wscript.exe for such a script because if you did, the script would display a separate dialog box for each echoed string. Also, some scripts need access to certain properties of the WScript object that don't exist when the script host is wscript.exe (e.g., the StdIn, StdOut, and StdErr properties).

To allow my scripts to determine the script host, I wrote the ScriptHost function. Callout A in Listing 1 shows the VBScript version of this function. Callout A in Listing 2 shows the JScript version.

Both versions of the ScriptHost function use the WScript object's FullName property, which returns the fully qualified path and filename of the script host. The VBScript and JScript functions then use language-appropriate methods for extracting the filename and lowercasing it.

The VBScript version uses the Mid function to extract the filename. This function returns a substring from a string and has three arguments. The first argument specifies the string from which you want to extract the substring. In this case, it's the string returned by the FullName property. The second argument specifies the point at which to start extracting the substring. In this case, the Mid function starts two characters past the number of characters in the string returned by the WScript object's Path property. The Path property's string specifies the directory containing the script host. You need to start two characters past the Path property's length for two reasons. First, the Mid function counts characters starting with the value of 1 rather than 0. Second, the Path property doesn't return the backslash character (\) that separates the path from the filename. The third argument specifies the number of characters to return. Including this argument is optional. If you omit it, the Mid function returns the remainder of the string. In this case, the argument is omitted, so the remainder of the substring is returned. The ScriptHost function then uses VBScript's LCase function to lowercase that substring.

The JScript version of ScriptHost function uses the same methodology as the VBScript version. The JScript version uses the substr method to return the path portion of the string returned by the WScript object's FullName property. JScript strings are 0-based rather than 1-based, so the ScriptHost function needs to count only one character past the number of characters returned by the WScript object's Path property to account for the backslash character. The function then uses the toLowerCase method to lowercase the resulting substring.

Listing 3 and Listing 4 demonstrate how you can use the ScriptHost function in VBScript and JScript code, respectively, to abort a script if it's not being run by cscript.exe. In both listings, the code uses the WScript object's Quit method to abort the script.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.