Freigeben über


Creating logon scripts

Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1, Windows Server 2003 with SP2

Creating logon scripts

You can use logon scripts to assign tasks that will be performed when a user logs on to a particular computer. The scripts can carry out operating system commands, set system environment variables, and call other scripts or executable programs. The Windows Server 2003 family supports two scripting environments: the command processor runs files containing batch language commands, and Windows Script Host (WSH) runs files containing Microsoft Visual Basic Scripting Edition (VBScript) or Jscript commands. You can use a text editor to create logon scripts. Some tasks commonly performed by logon scripts include:

  • Mapping network drives.

  • Installing and setting a user's default printer.

  • Collecting computer system information.

  • Updating virus signatures.

  • Updating software.

The following example logon script contains VBScript commands that use Active Directory Service Interfaces (ADSI) to perform three common tasks based on a user's group membership:

  1. It maps the H: drive to the home directory of the user by calling the WSH Network object's MapNetworkDrive method in combination with the WSH Network object's UserName property.

  2. It uses the ADSI IADsADSystemInfo object to obtain the current user's distinguished name, which in turn is used to connect to the corresponding user object in Active Directory. Once the connection is established, the list of groups the user is a member of is retrieved by using the user's memberOf attribute. The multivalued list of group names is joined into a single string by using VBScript's Join function to make it easier to search for target group names.

  3. If the current user is a member of one of the three groups defined at the top of the script, then the script maps the user's G: drive to the group shared drive, and sets the user's default printer to be the group printer.

To create an example logon script

  1. Open Notepad.

  2. Copy and paste, or type, the following:

    Const ENGINEERING_GROUP     = "cn=engineering"
    Const FINANCE_GROUP         = "cn=finance"
    Const HUMAN_RESOURCES_GROUP = "cn=human resources"
    
    Set wshNetwork = CreateObject("WScript.Network")
    wshNetwork.MapNetworkDrive "h:",
    "\\FileServer\Users\" & wshNetwork.UserName
    
    Set ADSysInfo = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" &
    ADSysInfo.UserName)
    strGroups = LCase(Join(CurrentUser.MemberOf))
    
    If InStr(strGroups, ENGINEERING_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Engineering\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\EngLaser"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\Plotter"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\EngLaser"
    
    ElseIf InStr(strGroups, FINANCE_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Finance\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\FinLaser"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\FinLaser"
    
    ElseIf InStr(strGroups, HUMAN_RESOURCES_GROUP) Then
    
        wshNetwork.MapNetworkDrive "g:",
        "\\FileServer\Human Resources\"
        wshNetwork.AddWindowsPrinterConnection
        "\\PrintServer\HrLaser"
        wshNetWork.SetDefaultPrinter
        "\\PrintServer\HrLaser"
    
    End If
    
  3. On the File menu, click Save As.

  4. In Save in, click the directory that corresponds to the domain controller's Netlogon shared folder (usually SystemRoot\SYSVOL\Sysvol\DomainName\Scripts where DomainName is the domain's fully qualified domain name).

  5. In Save as type, click All Files.

  6. In File name, type a file name, followed by .vbs, and then click Save. WSH uses the .vbs extension to identify files that contain VBScript commands.

Notes

  • To open Notepad, click Start, point to All programs, point to Accessories, and then click Notepad.

  • To use the example logon script, you need to change the group names, network drive letters, and Universal Naming Convention (UNC) paths to match your system environment.

  • To run a logon script, you need to assign the script to a user or a group. For more information, see Assign a logon script to a user or group.

For more information about creating and using logon scripts, see Logon Scripts, Windows Script at the Microsoft Web site, and the Microsoft Windows Resource Kits Web site.

Information about functional differences

  • Your server might function differently based on the version and edition of the operating system that is installed, your account permissions, and your menu settings. For more information, see Viewing Help on the Web.