Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 22, 2007

Copy Group Memberships from One AD User to Another

Script not only saves time but also includes a reusable function that converts usernames into DNs
RSS
Subscribe to Windows IT Pro | See More Active Directory (AD) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Executive Summary:
Using the Microsoft Management Console (MMC) Active Directory Users and Computer snap-in to copy group memberships in Microsoft Active Directory can be very time-consuming. To save time, you can use CopyMembership.vbs to automatically copy group membership from one Microsoft Active Directory user to another. CopyMembership.vbs uses Microsoft Active Directory Service Interfaces (ADSI) and Windows Script Host (WSH) 5.6.

My company's Security ID Admin team manages more than 5,000 employees. When a new employee is hired, the human resources (HR) department notifies us and we quickly create an employee object in Active Directory (AD). The HR department doesn't know the application or group memberships that should be assigned to the new employee, so we just assign the sAMAccountName (i.e., username) and don't include any membership information. After the new employee starts work, his or her department manager tells the Security ID Admin team to assign global-group roles based on the group memberships of existing employees in the department.

To assign global-group roles, the Security ID Admin team had to check the group memberships of an existing employee in the Microsoft Management Console (MMC) Active Directory Users and Computer snap-in, write down the information on a piece of paper, then add those membership groups to the new employee object. This process was very time-consuming. Thus, I wrote CopyMembership.vbs to automatically copy group membership from one AD user to another AD user. By design, this script doesn't remove any group memberships after the copy operation; it leaves the existing employee's groups intact because the script is used for new hires only.

I wrote and tested CopyMembership.vbs on machines running Windows XP. You need Active Directory Service Interfaces (ADSI) and Windows Script Host (WSH) 5.6 on the machine from which you want to run the script. Assuming CScript is the default engine, you use the following syntax to launch it:

Copymembership.vbs ntid1 ntid2 

where ntid1 is the existing employee's username and ntid2 is the new employee's username.

As Listing 1 shows, CopyMembership.vbs retrieves the usernames provided on command line, then converts them into distinguished names (DNs). The script calls the dn function to perform this conversion, as callouts A and B show. The dn function in callout D has proved to be quite handy. I've used it in other AD-related scripts, including AddComputers.vbs, which I discuss in "An MMC-less Way to Add Computers to Global Groups" (http://www.scriptingprovip.com/articles/articleid/95164/95164.html). As I describe in that article, the dn function uses ADSI's NameTranslate object. This object translates ADsPath entries from one format to another in the LDAP namespace. (For more information about this object, go to the "ADSI Objects of LDAP" Web page at http://msdn2.microsoft.com/en-gb/library/aa772208.aspx.)

After the two usernames are converted into DNs, CopyMembership.vbs binds to the LDAP namespace in AD and checks to see whether the two employee objects exist. If the script finds that one of the objects doesn't exist, it displays an error message and quits. When both employee objects exist, the script retrieves the group memberships from the object representing the existing employee and copies those memberships to the object representing the new employee, as callout C shows.

The Security ID Admin team has found CopyMembership.vbs to be a real time-saver. Even if you don't need to copy users' group memberships, you'll likely find the dn function a handy piece of code for your scripting toolbox.

—James Lim

Share Your Scripting Experiences
Share your scripting discoveries, comments, solutions to problems, and experiences with products. Email your contributions to r2r@scriptingprovip.com. Please include your full name and phone number. We edit submissions for style, grammar, and length. If we print your submission, you’ll get $100.

End of Article



Reader Comments
Trying to read this article

pizzamaker74 February 03, 2008 (Article Rating: )


Trying to read this article

pizzamaker74 February 03, 2008 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

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

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Related Articles Using Saved Queries for Active Directory Management

An MMC-less Way to Add Computers to Global Groups

Determining the Expiration of AD Domain Passwords

Quickly Finding User Accounts Anywhere in an Active Directory Forest

Active Directory (AD) Whitepapers Meeting Compliance Objectives in SharePoint

Email Controls and Regulatory Compliance

Continuous Data Protection and Recovery for Microsoft Exchange

Related Events Troubleshooting Active Directory

The Easiest Way to Save Time and Money on E-mail and SharePoint Management

7 Ways To Get More From Your SharePoint Deployment Now

Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks The Essentials Series: Active Directory 2008 Operations

Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement