Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


February 1999

WSH Logon Scripts


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here
Download the Code Here
Download the Code Here

A common job WSH can do for you

Have you postponed rolling out Windows Scripting Host (WSH) because you haven't figured out how your company can use the product? By migrating your logon environment to WSH, you'll be able to schedule and run smarter, more robust scripts on your enterprise's clients, and you'll prepare your firm to take advantage of Windows 2000 (Win2K$#151;formerly Windows NT 5.0). But maybe you're looking for a specific task for WSH to perform before deploying the product across your enterprise.

One common function that you might want to use WSH for is logging on NT users. To translate your current NT logon script to WSH, you need to take three easy steps: Install WSH on your client systems, create a wrapper batch file to run the WSH logon script, and convert existing batch files to Visual Basic Script (VBScript) or JScript.

Installing WSH Throughout Your Enterprise
As you probably already know, the NT 4.0 logon process doesn't support WSH scripts. (For more information about WSH, see "NT Gains Scripting Muscle," April 1998, and Keith Pleas, "Windows Scripting Host in Action," February 1998.) Therefore, you need to install WSH on each client that will run WSH logon scripts. You can use a standard NT logon script to test whether a system already has WSH installed, and to install WSH if the software isn't on the system.

If you're using NT's Directory Replicator service, your first step in distributing WSH to your clients is copying WSH's self-extracting archive, wsh.exe, to the %systemroot%\system32\Repl\Export\Scripts subdirectory on your export server. (For more information about the Directory Replicator service, see Mike Reilly, "Replication in Windows NT," page 187, and Bob Chronister, Tricks & Traps, September 1997.) The export server is generally the Primary Domain Controller (PDC) in an organization's account domain. You can obtain the WSH archive from http://msdn.microsoft.com/scripting.

Next, create a logon script similar to start.bat, which Listing 1, page 192, shows, or include the code at callout A in Listing 1 in your standard logon script. The code at callout A tests whether the client has the Windows-based WSH host, wscript.exe, installed. If start.bat doesn't find wscript.exe, the script launches WSH setup using the quiet option (as the /q switch indicates). The only caveat to this installation approach is that the user account that executes the logon script must have administrative rights on the client machine. If the user doesn't have administrative privileges, the WSH object model control (wshom.ocx), which exposes WSH's Network and Shell objects, fails to register.

Creating the Wrapper File
When you convert your current logon script to WSH, you'll need to embed that WSH script in a wrapper batch file because the NT 4.0 logon process doesn't support WSH-compliant ActiveX scripting. (Win2K will support WSH-compliant ActiveX scripting.) By default, you can run only batch files (files with a .bat or .cmd extension) or executables during an NT 4.0 logon. As start.bat demonstrates, you can include the wrapper code for your WSH logon script in the NT 4.0 logon script batch file you create to verify that client systems contain WSH. At callout B in Listing 1, start.bat calls my VBScript logon script, logon.vbs.

You can use wscript.exe or the console-based WSH host, cscript.exe, to run your WSH logon script. From a logon-script perspective, the key difference between the two is that only cscript.exe can use the ERRORLEVEL environment variable of the NT command processor, cmd.exe. ERRORLEVEL is a built-in variable that contains the exit code from the last command that ran via the command processor. If you want to use WScript's Quit method to return an exit code that the wrapper batch file can test, you must use cscript.exe to run your script.

Rewriting Existing Scripts
After you install WSH on the target clients and decide how to run your WSH script from a batch file, you're ready to start migrating your existing scripts to WSH. The first decision you'll have to make in this migration is whether to write your scripts in VBScript or JScript, the two ActiveX scripting engines that Microsoft packages with WSH. The decision between VBScript and JScript is mostly a matter of preference, but the two languages differ significantly in their syntax and functionality. For example, VBScript looks and feels like Visual Basic (VB); JScript closely resembles C and Java. JScript supports regular expressions, but VBScript does not (although Microsoft included support for regular expressions in the VBScript 5.0 engine, which was in beta at press time). I prefer VBScript for logon scripts because of the language's ease of use, its similarity to VB, and its strong component object model (COM) support. When I write logon scripts, I'm not concerned about cross-platform browser support, which was the initial problem that JScript (and JavaScript) sought to resolve.

You need to become familiar with three scripting tasks to successfully rewrite your current logon scripts in VBScript: executing external commands; replacing NT's net.exe commands with the corresponding WSH objects, methods, and properties; and replacing Microsoft Windows NT Resource Kit or third-party commands and utilities with functionality that the WSH object model provides.

As you work through the migration exercise, you'll find that WSH doesn't provide functionality equivalent to every command or utility you use. Therefore, you must use external commands to accomplish some of your current logon scripts' tasks. For example, WSH doesn't provide a time synchronization object to replace NT's NET TIME command. To use your WSH logon script to synchronize clients' time with the time on the domain that logs them on, you must use the WSH Shell object's Run method to execute an external command.

In addition, when you implement WSH logon scripts, you'll discover tasks that command-line or resource kit utilities perform but that you must add to your WSH scripts through object libraries such as Active Directory Service Interfaces (ADSI). For example, if you convert to WSH a batch file that uses the ifmember.exe resource kit utility to test for a user's group membership, you can replace ifmember.exe with ADSI's Group object and corresponding IsMember method. (I'll explore leveraging ADSI in logon scripts in a future column.)

As you look for WSH substitutes for batch file functions, keep in mind that the engine of the script you use provides a number of useful objects. For example, VBScript offers FileSystemObject, Err, and Dictionary objects. (For more information about VBScript Dictionary objects, see "Leveraging Components," January 1999.) Table 1, page 194, highlights approaches for accomplishing common logon script tasks in batch files and the corresponding solutions in WSH.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Getting your iPhone to Sync with Exchange 2003

Follow these steps to use an iPhone with Exchange. ...


Task Automation Whitepapers Three Ways to Prevent Insider Risk within Your SCCM-Managed Environment

From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement