共用方式為


Creating administrative scripts

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

Creating administrative scripts

With scripts, you can automate a wide range of common administrative tasks, such as:

  • Administering users, groups, and organizational units.

  • Configuring computers.

  • Managing event logs.

  • Maintaining files, folders, and disks.

  • Monitoring performance, processes, and services.

The Windows Server 2003 family supports two scripting environments:

  • The command processor runs files that contain batch language commands. Batch language has limited capabilities, but earlier operating systems supported it and you may need to continue using existing batch files. For more information about batch files, see Using batch files.

  • Windows Script Host (WSH) runs files that contain Microsoft Visual Basic Scripting Edition (VBScript) or Jscript commands. VBScript and Jscript provide all the capabilities of batch language plus many more. For example, scripts written in VBScript or Jscript can interact with Active Directory Service Interfaces (ADSI) to manage objects stored in Active Directory, and they can interact with Windows Management Instrumentation (WMI) to access system resources.

In addition to the two scripting languages, WSH provides two runtime programs: WScript.exe and CScript.exe. After you create a WSH script containing VBScript or Jscript commands, you use WScript or CScript to run the script. WScript runs the script as a Windows-based process and CScript runs the script as a console-based process.

Administrators often want to manage objects in Active Directory, such as organizational units, groups, and users. The following example script shows how you can use WSH, VBScript, and ADSI to create an Active Directory organizational unit, group, and user. After creating the three Active Directory objects in the current domain, the script assigns a password to the new user account, enables the user account, and adds the user account to the group.

To create an example script

  1. Open Notepad.

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

    Set oRoot   = GetObject("LDAP://rootDSE")
    Set oDomain = GetObject("LDAP://" &
    oRoot.Get("defaultNamingContext"))
    
    Set oOU=oDomain.Create("organizationalUnit",
    "ou=Demo OU")
    oOU.Put "Description", "Demonstration OU"
    oOU.SetInfo
    
    Set oGroup = oOU.Create("Group", "cn=Demo
    Group")
    oGroup.Put "sAMAccountName", "DemoGroup"
    oGroup.Put "Description", "Demonstration
    Group"
    oGroup.SetInfo
    
    Set oUser = oOU.Create("User", "cn=Demo User")
    oUser.Put "sAMAccountName", "DemoUser"
    oUser.Put "Description", "Demonstration User"
    oUser.SetInfo
    
    oUser.SetPassword "qW5rty"
    
    oUser.AccountDisabled = False
    oUser.SetInfo
    
    oGroup.Add oUser.ADSPath
    
  3. On the File menu, click Save As.

  4. In Save as type, click All Files.

  5. In File name, type a file name, followed by .vbs, and then click Save. The file is saved as a VBScript.

Notes

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

  • The root of the current domain cannot contain an organizational unit named Demo OU.

To run the example script

  1. Open Command Prompt.

  2. Change to the directory where the script is located.

  3. Type the following:

    wscriptScriptName**.vbs**

    where ScriptName**.vbs** is the name of the script.

Notes

  • To perform this procedure, you must be a member of the Administrators group on the local computer, or you must have been delegated the appropriate authority. If the computer is joined to a domain, members of the Domain Admins group might be able to perform this procedure. As a security best practice, consider using Run as to perform this procedure.

  • To open a command prompt, click Start, point to All programs, point to Accessories, and then click Command prompt.

  • This example starts the script from the command prompt. You can also start the script by clicking Start, clicking Run, typing wscript ScriptName**.vbs** in Open, and then clicking OK.

  • This example runs the script as a Windows-based process. To run it as a console process, type cscript instead of wscript.

For more information, see Windows Script Host, Windows Script at the Microsoft Web site, and the System Administration Scripting Guide at 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.