Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 


Reader Comments
I dont understand something please. If I type %OS% in the run box on a XP and 2000 machine it returns "Windows_NT" I am missing something intrinsic to this method. Wouldn't it never fail if XP and 2000?

Thx in advance ,

Tony August 28, 2003


XP and 2000 run the nt kernel so they are considered nt for an os...

Troy November 24, 2003


i want run exe file using client vbscript (how use wscript.shell?)
what i do to run exe file on client machin from server-site?
thank u for help me or not help me

fahd elhariry December 17, 2003


Where do I place the logon scripts on a Windows Server 2003 machine for each domain user?

Pedro April 22, 2004


Hello,
How can i create an exe file from an ASP file

Rajitha April 25, 2004


\windows\sysvol\sysvol\scripts\

Noel April 26, 2004


Hello everyone, can anyone tell me how to protect the scripts from evil users. As I understand those logon scripts have to be available for the client, but then if they want to misuse it, or delete it they also can, that is a no no. So I am wondering what I can do. Thanks

Nile May 01, 2004


yeah Niles, put them in your sysvol or netlogon, then only admins will have write permission

matt May 25, 2004


Is there a way to run a login script that will display date and time of last login; the workstation ID of last login; and number of unsuccessful login attempts since last successful? I have a regulation I need to meet and cannot find a good solution for this requirement.

Anita June 09, 2004


Has anyone created vb scripts and successfully deployed them in a Windows98 section enviornment. I am running 2000 server.

Bret June 18, 2004


 See More Comments  1   2 

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Next Version of Exchange Named Exchange 2010?

Microsoft apparently inadvertently announced the official name of the next version of Exchange Server. ...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events 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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing