Using VBScript to Automate User and Group Administration



Similar documents
Module 4: Implementing User, Group, and Computer Accounts

ShoreTel Active Directory Import Application

Module 4. Managing Groups. Contents: Lesson 1: Overview of Groups 4-3. Lesson 2: Administer Groups Lab A: Administer Groups 4-36

Step-by-Step Guide to Active Directory Bulk Import and Export

CHAPTER THREE. Managing Groups

HOW TO: Customise the style of the display name in Active Directory Users and Computers and the GAL

ShoreTel Active Directory Import Application

Using LDAP Authentication in a PowerCenter Domain

Windows Server 2003 Logon Scripts Paul Flynn

Active Directory Commands ( )

AD Ou structure generated by Adsync process

LDAP and Active Directory Guide

Module 3: Implementing an Organizational Unit Structure

Integrating Webalo with LDAP or Active Directory

Active Directory LDAP Quota and Admin account authentication and management

Step-by-Step Guide to Bulk Import and Export to Active Directory

Administrator's Guide

LDAP Directory Integration with Cisco Unity Connection

Restructuring Active Directory Domains Within a Forest

User Migration Tool. Note. Staging Guide for Cisco Unified ICM/Contact Center Enterprise & Hosted Release 9.0(1) 1

SOFTWARE BEST PRACTICES

11 essential tools for managing Active Directory

Configuring and Using the TMM with LDAP / Active Directory

Technical Bulletin 005 Revised 2010/12/10

[MS-FSADSA]: Active Directory Search Authorization Protocol Specification

Ultimus and Microsoft Active Directory

How To Configure The Active Directory Module In Sitecore Cms (For A Web.Com User)

Active Directory Friday: All Articles. Jaap Brasser

IMPLEMENTING DIRECTORY SERVICES INTEGRATION WITH HELIX MEDIA LIBRARY Revision Date: September 2014

ADMT v3 Migration Guide

Configure Directory Integration

Océ LDAP Adapter User Guide

ADMT v3.1 Guide: Migrating and Restructuring Active Directory Domains

Quality Center LDAP Guide

TechJam Active Directory Auditing Presenter Matt Warburton Professional Services

Cloudwork Dashboard User Manual

How To Authenticate On An Xtma On A Pc Or Mac Or Ipad (For A Mac) On A Network With A Password Protected (For An Ipad) On An Ipa Or Ipa (For Mac) With A Log

Identity Management in Quercus. CampusIT_QUERCUS

Microsoft Visual Basic Scripting Edition and Microsoft Windows Script Host Essentials

Microsoft Virtual Labs. Active Directory New User Interface

Technical Overview. Active Directory Synchronization

Module 1: Introduction to Active Directory Infrastructure

Chapter 4: Implementing and Managing Group and Computer Accounts. Objectives

Configuring Windows Server 2008 Active Directory

Introduction Installing and Configuring the LDAP Server Configuring Yealink IP Phones Using LDAP Phonebook...

The following gives an overview of LDAP from a user's perspective.

User Management Resource Administrator. UMRA tables. User Guide

Administering Active Directory Administering W2K Server

Module 2: Managing User and Computer Accounts

Active Directory at the University of Michgan. The Michigan Way Since 2000

PGP Desktop LDAP Enterprise Enrollment

R4: Configuring Windows Server 2008 Active Directory

Quick Introduction System Requirements Main features Getting Started Connecting to Active Directory... 4

Adeptia Suite LDAP Integration Guide

JiJi Active Directory Reports JiJi Active Directory Reports User Manual

EVERYTHING LDAP. Gabriella Davis

Novell Identity Manager

Administrator's Guide

Integration Guide. SafeNet Authentication Service. Integrating Active Directory Lightweight Services

RSA Authentication Manager 7.1 Microsoft Active Directory Integration Guide

Installation and Configuration Guide

LISTSERV LDAP Documentation

Field Description Example. IP address of your DNS server. It is used to resolve fully qualified domain names

Sample Configuration: Cisco UCS, LDAP and Active Directory

SonicOS Enhanced 3.2 LDAP Integration with Microsoft Active Directory and Novell edirectory Support

Automatic Deployment and Authentication Guide

HELP DOCUMENTATION UMRA REFERENCE GUIDE

Integrate with Directory Sources

Admin Report Kit for Active Directory

Polycom RealPresence Resource Manager System Administrator s Guide

Configuring Sponsor Authentication

Chapter 1 Manage Users, Computers and Groups...2. Chapter 2 Managing and Maintaining Access to Resources...43

BlackShield ID. QUICKStart Guide. Integrating Active Directory Lightweight Services

Active Directory Adapter with 64-bit Support User Guide

Your Question. Article: Question: How do I Configure LDAP with Net Report?

How To Take Advantage Of Active Directory Support In Groupwise 2014

KACE Appliance LDAP Reference Guide V1.4

Microsoft Windows PowerShell v2 For Administrators

Active Directory Sync (AD) How it Works in WhosOnLocation

INUVIKA OVD VIRTUAL DESKTOP ENTERPRISE

ECAT SWE Exchange Customer Administration Tool Web Interface User Guide Version 6.7

Setting up LDAP settings for LiveCycle Workflow Business Activity Monitor

NewsletterAdmin 2.4 Setup Manual

Usage: admodcmd [-dn BaseDN] [-p pagesize] [-s] [-server servername] [-f LDAPFilter] [modification]

Creating Organizational Units, Accounts, and Groups. Active Directory Users and Computers (ADUC) 21/05/2013

How To Search For An Active Directory On Goprint Ggprint Goprint.Org (Geoprint) (Georgos4) (Goprint) And Gopprint.Org Gop Print.Org

ACTIVE DIRECTORY WEB SERVICE USER GUIDE LAST UPDATED: January 4, 2013

Manage Fine-Grained Password and Account Lockout Policies

PriveonLabs Research. Cisco Security Agent Protection Series:

Active Directory 2008 Audit Management Pack Guide for Operations Manager 2007 and Essentials 2010

Active Directory Cleaner User Guide 1. Active Directory Cleaner User Guide

Configuring Microsoft Active Directory for Integration with NextPage NXT 3 Access Control

Here, we will discuss step-by-step procedure for enabling LDAP Authentication.

Quick Connect Express for Active Directory

StarTeam/CaliberRM LDAP QuickStart Manager Administration Guide

How To Set Up A Webmin Account On A Libc (Libc) On A Linux Server On A Windows 7.5 (Amd) With A Password Protected Password Protected (Windows) On An Ubuntu (Amd

Transcription:

Using VBScript to Automate User and Group Administration Exam Objectives in this Chapter: Create and manage groups Create and modify groups by using automation Create and manage user accounts Create and modify user accounts by using automation In Chapters 3 and 4, you learned how to create and manage user and group accounts using the Active Directory Users and Computers snap-in, directory service commandline commands, CSVDE, and LDIFDE. These skills and concepts will be tremendously valuable on a day-to-day basis. Occasionally, however, you will be faced with a task for which those techniques might be insufficient. For example, you might wish to modify properties of a user object that are not accessible through DSMOD parameters. The Windows Scripting Host (WSH) enables automation of both simple and sophisticated administrative tasks. It interprets scripts much like the command shell (cmd.exe) interprets batch files. WSH supports scripting in VBScript: a relatively straightforward scripting language that you can use to perform tasks on almost every component of Windows Server 2003, including Active Directory directory service. Although a thorough understanding of VBScript and its capabilities would be a tremendous benefit to you as a system administrator, that level of detail is beyond the scope of the 70-290 exam and of this book. You do need to be able to recognize scripts that can perform useful, day-to-day user and group management tasks. We will go slightly farther than that in our discussion, as we introduce you to scripting terminology and concepts that will help you understand exactly what the scripts are doing. Do your best to analyze and understand the way the scripts are working, but take comfort that the examination will expect you only to be able to recognize what a script actually does. VBScript files are typically named with a.vbs extension. If you open a.vbs file using Windows Explorer or from the command prompt using either Cscript.exe or Wscript.exe, WSH interprets the script and performs each command in the script. 1-1

1-2 Creating a script in Notepad To create a script, simply open Notepad, type the script, and then save the script with a.vbs extension. Here s a tip: when you choose the Save command in Notepad, surround the filename with quotation marks and include the extension, such as: Myscript.vbs. Without the quotation marks, Notepad may add a.txt extension, resulting in a filename such as Myscript.vbs.txt, which will not open in WSH. Creating Users and Groups with VBScript Let s start our examination of Active Directory scripts and VBScript by looking at two functioning scripts. The first creates a user account in the Management OU of the contoso.com domain. The second creates a global group account. Notice their similarities. Create a user account Set objou = GetObject("LDAP://OU=Management,dc=contoso,dc=com") Set objuser = objou.create("user", "cn=dan Holme") objuser.put "samaccountname", "DanHolme" objuser.setinfo Create a global group account Set objou = GetObject("LDAP://OU=Management,dc=contoso,dc=com") Set objgroup = objou.create("group", "cn=phx-users") objgroup.put "samaccountname", "phx-users" objgroup.setinfo VBScript is referred to as an object-oriented scripting language. Objects are virtual representations of a component of the system. Instead of performing an action directly on the system, you perform the action on the object that represents the system. Instead of setting or querying a property directly from the system, you get or set the property of the object that represents the system. For example, you ll notice that the first line of each script is the same: Set objou = GetObject("LDAP://OU=Management,dc=contoso,dc=com") The line is creating what is called an object reference: it is creating the virtual representation of a system component as just mentioned. In this case, an object called objou is being configured (Set) to represent (=) the Management OU in the contoso.com domain. In order to point objou to the Management OU, a method called GetObject queries Active Directory using Lightweight Directory Access Protocol (LDAP) and the distinguished name (DN )of the Management OU. You can safely think of methods as commands or actions. The GetObject method is a native method for WSH and is used regularly to find a specific system component so that an object can be set to refer to that component.

The object name, in this case objou, is irrelevant you could call it almost anything you want, such as Bob, as long as you don t use a name that is also used as a command in VBScript. That s a reasonable limitation that allows you to name things just about any way you want. It is a convention, rather than a requirement, that objects begin with a prefix such as obj or o to indicate the type of named element. Each object, once created, supports methods specific to the type of system component that the object represents. As you know, you can create users, groups, or any number of other Active Directory objects within an OU. Therefore, not surprisingly, the reference objou that represents an OU supports a method to create objects. That method is creatively named Create. Notice the similarity between the two scripts: Set objuser = objou.create("user", "cn=dan Holme") Set objgroup = objou.create("group", "cn=phx-users") In both scripts a new object reference is being set. Again, the names of the objects (objuser and objgroup) are irrelevant, but the convention is to prefix the name with obj and then to use an identifiable name such as User or Group. This time the object references are representing something that hasn t existed yet: we are creating a new object within the Management OU, represented by objou. As mentioned above, objects referring to OUs support a Create method. We simply have to specify what type of object we are creating and what its common name (CN) will be. Using the Create method does not write any data to Active Directory. Why? Because object references are virtual representations everything is happening outside of the actual system component (the OU) so far. And that s a good thing, because at this point in the scripts, the objects would fail to be created anyway because mandatory properties of the objects have not yet been configured. The CN of an object is a mandatory property and is specified when you use the Create method. Some mandatory properties of users and groups are configured automatically by the system. For example, the security identifier (SID) of the object is created automatically. There is one other mandatory property of user and group objects that must be specified before the object can be created, and that is its SAM Account Name. As you learned in Chapter 3, the SAM Account Name is the downlevel account name for users, groups, and computers, used for compatibility with pre Windows 2000 operating systems. The third lines of the scripts configure the SAM Account Names: objuser.put "samaccountname", "DanHolme" objgroup.put "samaccountname", "phx-users" Now that the object references to the new objects have been created (objuser and obj- Group), those objects allow us to set or get properties of the objects, using the Put and Get methods, respectively. The Put method is followed by the LDAP attribute name 1-3

1-4 (samaccountname) and the value that we want the attribute to take on (Dan Holme and phx-users). Again, all of this is happening virtually in objects that exist in the computer s memory only. The final step in a script to create a user or group is to write the information to Active Directory, which is achieved using the user or group object s SetInfo method: objuser.setinfo objgroup.setinfo Voílà! A user and group are created in Active Directory! You might think that this is a lot of work, compared to right-clicking and choosing New User in the Active Directory Users and Computers snap-in. However, with a little work, you can write a script that pulls information from a database and creates accounts based on that information. As we stated at the beginning of the chapter, with each task you perform as an administrator you should evaluate the time required to do the task manually and accurately against the time it takes to create and test the functionality and accuracy of a script. Managing Users and Groups with VBScript The remainder of this chapter will present sample scripts that perform routine administrative tasks for user and group accounts. We will touch on the behind the scenes workings of the script, but it is only necessary that you are able to recognize what the scripts achieve. Add a user to a group Set objgroup = GetObject _ ("LDAP://cn=phx-users,OU=Management,dc=contoso,dc=com") objgroup.add "LDAP://cn=Dan Holme,OU=Management,dc=contoso,dc=com" This script adds the user we created (Dan Holme) to the group we created (phx-users). To read the script, you must know that the underscore character ( _ ) used at the end of a line indicates that the command is continued on the following line. It is a continuation marker. So the first two lines of the script are actually one command that creates an object that refers to the phx-users group. The second command, on the last line, adds the user to the group. That action is achieved by using the Add method supported by objects that refer to groups and pointing to the DN of the user account. Can you guess how we would remove a user from a group? The exact same script, with the Remove method of the group object, achieves that task. Scripting Resources To learn more about scripting, objects, methods, and properties, visit http:// www.microsoft.com/technet/scriptcenter, or for even more detail, explore http:// msdn.microsoft.com.

1-5 Remove a user from a group Set objgroup = GetObject _ ("LDAP://cn=phx-users,OU=Management,dc=contoso,dc=com") objgroup.remove "LDAP://cn=Dan Holme,OU=Management,dc=contoso,dc=com" After you ve created a user or group object, you will want to be able to configure properties of the object. There are two primary formats for doing this. For most properties the syntax is: objuser.put "property name", "property value" For example, the following script sets a number of common user properties: Configure common user properties objuser.put "givenname", "Dan" objuser.put "initials", "F." objuser.put "sn", "Holme" objuser.put "userprincipalname", "DanHolme@contoso.com" objuser.put "displayname", "Dan Holme" objuser.put "title", "Marketing Manager" objuser.put "profilepath", "\\phxsrv01\profiles\danholme" objuser.put "homedirectory", "\\phxsrv01\homefolders\danholme" objuser.put "homedrive", "H:" objuser.put "scriptpath", "logon.vbs" objuser.put "telephonenumber", "(425) 555-1211" objuser.put "mail", "DanHolme@contoso.com" objuser.put "wwwhomepage", "http://www.contoso.com" objuser.put "physicaldeliveryofficename", "Room 4358" objuser.setinfo Some user properties are stored in arrays. An array is a structure that holds more than a single value. For example, a user can have several telephone numbers, so the othertelephone attribute is declared to be multivalued, or an array. To store or modify values in an array, you use the PutEx, or put extended method of the user object. PutEx can use a total of four operations. These operations are clear, update, append, or delete, and each are declared by using either their individually assigned value or constant a name for a value. For example, to replace the value of a specified attribute, PutEx is followed by the assigned number 2 or by the constant ADS_PROPERTY_UPDATE, which indicates an update, followed by the property name and the value(s) passed in an array. The following script sets the PutEx update constant called ADS_PROPERTY_UPDATE. The PutEx constant is used in the script instead of the actual number 2. This is purely convention. The theory is that it is easier to understand what the script is doing when you read a name like ADS_PROPERTY_UPDATE than it is to see the number 2 and be unsure what that number indicates. (For an explanation of PutEx operations, refer to Reading and Writing User Account Attributes in the online Windows 2000 Scripting Guide at http://www.microsoft.com/technet/script center/guide/sas_usr_nehi.mspx).

1-6 Configure common user properties Const ADS_PROPERTY_UPDATE = 2 objuser.putex ADS_PROPERTY_UPDATE, _ "description", Array("Management staff") objuser.putex ADS_PROPERTY_UPDATE, _ "othertelephone", Array("(800) 555-1212", "(425) 555-1213") objuser.putex ADS_PROPERTY_UPDATE, _ "url", Array("http://www.contoso.com/management") objuser.setinfo To set a user s password, use the SetPassword method of the user object. Note that unlike setting other properties of a user, you do not use SetInfo to write the password to Active Directory: password changes are written directly to the directory by the Set- Info method. Set a user s password objuser.setpassword "i5a2sj*!" If you create a user account with a script, the user account will be disabled. After configuring appropriate logon credentials (the samaccountname, userprincipalname, and password), you need to enable the account using the following script. Note that the object must exist in the directory it must have been written to Active Directory with its first SetInfo method before it can be enabled or disabled. Enable a user account objuser.accountdisabled = FALSE objuser.setinfo The final scripts we ll present involve listing the members of a group and listing the groups to which a user belongs. These scripts involve more complex structures, including loops that repeat multiple times using a syntax that begins with a For statement and ends with a Next statement. They also involve displaying information using the WScript.Echo statement. The details of the scripts are beyond the requirements of the exam and the scope of this book, but you should look at the scripts and get a general feeling for what they accomplish. Each script is documented with comments that begin with an apostrophe ( ' ). List the members of a group ' This script lists the members of a group ' Note that it does not look for multiple levels of nesting ' It is only listing the direct membership '

1-7 ' Create an object representing the desired group Set objgroup = GetObject _ ("LDAP://cn=phx-users,OU=Management,dc=contoso,dc=com") ' Loop through each of the group's Members attribute For each objmember in objgroup.members ' Display the member Wscript.Echo objmember.name Next List the local groups to which a user belongs ' This script looks through the local groups on a computer ' and displays the names of groups to which a specified user ' belongs ' ' The following line sets a string variable to represent the ' name of the computer on which the script will run. ' Substitute a period "." for the computer name and the script ' will act on the local computer on which the script is executed strcomputer = "phxdesktop01" ' The following line sets a string variable to represent the ' name of the user we are looking for in local groups strusername = "danholme" ' The next two lines query the computer's *local* directory (SAM) ' and filter out all objects except groups Set colgroups = GetObject("WinNT://" & strcomputer & "") colgroups.filter = Array("group") ' The following line loops through each group that was found ' on the system For Each objgroup In colgroups ' The following loops through each user belonging ' to the group and checks to see if the user name is ' the one we're looking for For Each objuser in objgroup.members If objuser.name = strusername Then ' If we've found the user in the group, display ' the group name Wscript.Echo objgroup.name End If ' Loop to the next user in the group Next ' Loop to the next group on the computer Next List the groups to which a user belongs ' This script shows the groups to which a user belongs ' Note that it does not check for multiple levels of nesting ' It is only looking for groups to which the user *directly* ' belongs. It also does not show the user's Primary Group. ' See the next script for an example of how to show the ' primary group. ' ' Create an object referencing the specified user

1-8 ' Create an object representing the MemberOf attribute of the user objmemberof = objuser.getex("memberof") ' Display an introductory message WScript.Echo VbCrLf & "The user is a member of:" ' Loop through each group that is listed in the MemberOf attribute For Each Group in objmemberof ' Display that group Wscript.Echo Group Next List the groups to which a user belongs and the user s primary group ' This script is more complex. ' It lists all of the groups in Active Directory to which a user ' belongs directly (including the primary group) but does not ' show groups to which the user belongs indirectly through ' group nesting ' ' Indicate that we want to skip any errors On Error Resume Next ' Create a constant representing a common error value Const E_ADS_PROPERTY_NOT_FOUND = &h8000500d ' Create an object referencing the desired user ' Retrieve the list of groups to which the user directly belongs arrmemberof = objuser.getex("memberof") ' In case an error was returned, ' we evaluate whether the error returned ' indicates that the user belongs to no groups If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then WScript.Echo "The memberof attribute is not set." ' otherwise, we list the groups that the user belongs to Else WScript.Echo "Member of: " ' Loop through each group listed in the MemberOf attribute ' and display the group name For each Group in arrmemberof WScript.Echo Group Next Err.Clear End If ' The user's primary group (usually Domain Users) ' is not included in the MemberOf attribute ' These lines find the primary group, which is indicated by ' an integer in the primarygroupid attribute of the user, ' then take that integer and perform an Active Directory ' database query to locate the name that corresponds to that ' primary group ID ' Retrieve the user's Primary Group attribute intprimarygroupid = objuser.get("primarygroupid") Set objconnection = CreateObject("ADODB.Connection") objconnection.open "Provider=ADsDSOObject;" Set objcommand = CreateObject("ADODB.Command")

1-9 objcommand.activeconnection = objconnection objcommand.commandtext = _ "<LDAP://dc=contoso,dc=com>;(objectCategory=Group);" & _ "distinguishedname,primarygrouptoken;subtree" Set objrecordset = objcommand.execute While Not objrecordset.eof If objrecordset.fields("primarygrouptoken") = intprimarygroupid Then WScript.Echo "Primary group:" WScript.Echo objrecordset.fields("distinguishedname") & _ " (primarygroupid: " & intprimarygroupid & ")" End If objrecordset.movenext Wend objconnection.close As you can see from the last two examples, there is often more than one way to script a particular task. Scripting simple tasks is straightforward, and you should make every effort to use scripts and increase your comfort level and familiarity with scripting. You ll find that soon even more complex scripts are understandable. The best news of all is that Microsoft TechNet and MSDN provide numerous examples of scripts, and in most cases you need only to modify a few pieces of a sample script to make it work in your environment. Exam Highlights Key Points Before taking the exam, review the key points and terms that are presented below to help you identify topics you need to review. Return to the lessons for additional practice and for pointers to more information about topics covered by the exam objectives. Windows Script Host (WSH) supports VBScript files. VBScript is a relatively straightforward scripting language that enables you to script just about any task on a Windows Server 2003 system. Key Terms method An action or command that is performed on an object. The methods that are available for an object depend on the system component to which the object refers. For example, an object referring to an OU exposes the Create method to create new objects in the OU. An object referring to a group exposes the Add method to add members. object reference A virtual representation of a particular system component. In scripting, object references are created and then the object can be used to expose methods and properties.