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.
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
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
On the File menu, click Save As.
In Save as type, click All Files.
In File name, type a file name, followed by .vbs, and then click Save. The file is saved as a VBScript.
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
Open Command Prompt.
Change to the directory where the script is located.
Type the following:
where ScriptName**.vbs** is the name of the script.
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.
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.