Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


January 15, 2008

Using PowerShell to Manage Groups, Part 2

Save time by scripting Exchange group management tasks
RSS
Subscribe to Windows IT Pro | See More Windows OSs Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

In Exchange Server 2007, you can use Windows PowerShell commands via Exchange Management Shell to manage standard and dynamic groups. In “Using PowerShell to Manage Groups, Part 1,” September 2007, InstantDoc ID 96699, I covered some of the basics about Windows and Exchange groups and Exchange Management Shell commands. Plus you saw how PowerShell is used to enable standard groups and work with group properties. Now I want to show you how to use PowerShell to maintain group membership and work with dynamic groups.

Maintaining Group Membership
Exchange administrators regularly maintain group memberships. Working with the shell, you use the Add-DistributionGroupMember command to add a member to a group, like this:

Add-DistributionGroupMember -id `
'Editors' -Member 'Eoin Redmond'

You must provide a pointer to the new member that Exchange can resolve. This can be a distinguished name (DN) such as “CN=Alan Kerr, OU=Exchange, DC=XYZ, DC=COM,” a user principal name (UPN) such as Joe.Jones@xyz.com, an alias, or a display name (used in the example). If you have more than a few members to add, you can do so by using a basic PowerShell trick—create a table, then pipe the table as input to the Add-DistributionGroupMember command, as follows:

"Jack Smith", "Jane  Doe", `
   "Molly Maguire" | `
   Add-DistributionGroupMember `
   -id 'Editors'

You can also scan mailboxes and apply a filter to discover members you want to add to a group. Here’s a one-line command that scans for mailboxes belonging to the “New York” office, then adds them to a group:

Get-Mailbox –Filter `
 {Office -eq 'New York'} | `
Add-DistributionGroup `
-id 'New York Users'

Of course, you can also read in a list of members from a file and use the values to update a DG's membership. You can find many examples of code posted on Web sites and blogs. Try starting with

http://www.exchangeninjas.com/PSResources or http://blogs.technet.com/evand/

One of the joys of PowerShell is how easy it is to find and repurpose code for your needs, and I expect that we'll see more Web-based libraries of PowerShell examples for Exchange administrators in the next few years.

To check that your group has the right membership, use the Get-DistributionGroupMember command, as I’ve done in Listing 1. If you’ve made a mistake in adding members to the group, you can remove the incorrect entries by using the Remove-DistributionGroupMember command:

Remove-DistributionGroupMember `
   -id 'Editors' -Member  'Alan Nemeth'

The shell will prompt you to confirm that you want to perform the action before it removes the object.

You can also find out the groups to which a user belongs. Doing so is a bit complicated because of the way AD uses pointers to build group membership:

$Member = (Get-User -id `
   'Tony Redmond').Identity  ;
Get-Group -Filter {Members -eq  $Member}

There are actually two commands here. The Get-User command populates a variable called $Member with details of a user’s identity (the set of properties that lets Exchange find a user; Exchange is intelligent enough to select the most appropriate property from the set). Then the Get-Group command finds all groups that include the user. This command could be slow in large forests, but it’s a good example of how you can use PowerShell to find information that’s difficult to locate through the GUI.

Working with Dynamic Groups
Dynamic groups are different from standard distribution groups because they possess no membership until Exchange executes a query (called the recipient query) against AD to build the group membership. Exchange 2007 typically executes the query in the categorizer component of the transport service when messages addressed to dynamic groups flow through the first Hub Transport server. However, you can specify a server responsible for group expansion by updating the group’s properties, as Figure 1 shows.

You can easily create a new dynamic group by using the New-DynamicDistributionGroup command. This command puts together a dynamic DL for all mailboxes in the company, as in Listing 1. This command has two important parameters. First, the RecipientFilter parameter specifies the query Exchange executes against AD to build the group membership. For Company -eq ‘XYZ’ Exchange asks AD to find every entry that has XYZ in the company field. I also specify that I’m interested only in user mailboxes because I don’t want this list to be used to send messages to other groups or resource mailboxes. Second, the RecipientContainer parameter specifies the point in AD to start searching from, including all locations under this point. In this example, I pass the name of the domain, to include all of the mailboxes in the domain.

Exchange 2007 supports two types of recipient filters—precanned and custom. Precanned filters operate inside known boundaries, so Exchange optimizes the queries automatically when you use them. If you create or edit a query for a dynamic group through the Exchange Management Console wizard, the query will be precanned because the console limits your ability to create or edit queries to a set of known conditions presented by the wizard. Figure 2 shows how the console presents precanned conditions when you edit a dynamic group. A custom filter is one that you code yourself, as I did when I created the sample in Listing 2. Custom filters give you maximum flexibility in your Exchange-AD queries, but the syntax required to generate a precise OPath query (the standard filtering syntax used by PowerShell) can be hard to understand until you get used to it. For more information on O/PATH, go to http://www.exchangeninjas.com/OPATHSyntaxforDynamicDistributionGroups.

Exchange 2007 supports a set of special parameters—such as ConditionalCompany, ConditionalDepartment, and IncludedRecipients—that you can use to create precanned queries through Exchange Management Shell. If I want to change the custom query I used to create the dynamic group  for company users to a precanned query, I’d use the command in Listing 3.

You can check the details of precanned and custom queries to see the exact syntax generated by Exchange by using the Get-DynamicDistributionGroup command, as Listing 4 shows. Exchange Management Shell returns three properties from this command. RecipientFilter is the query in OPath format, LDAPRecipientFilter is the query in LDAP format, and RecipientFilterType tells you whether the query is precanned or custom.

Why does Exchange have queries in both OPath and LDAP format? Query-based groups in Exchange Server 2003 use LDAP-format queries, so Microsoft had to ensure that dynamic-based groups created on Exchange 2007 servers could function if they were expanded by Exchange 2003 servers. Behind the scenes, Exchange 2007 automatically translates the OPath syntax into LDAP format after you update a query. However, you can’t pass an LDAP-syntax query to Exchange 2007 and have it update a dynamic group because you must either use the precanned parameters or pass a complete recipient filter in OPath syntax. Also, you can’t edit an Exchange 2007 dynamic group using Exchange 2003's Exchange System Manager because Exchange 2003 is smart enough to understand that it doesn’t know anything about OPath. Every object has an ExchangeVersion property that tells you whether Exchange 2003 can edit it. If the property is “0.1 (8.0.535.0)” it means that you can use only Exchange 2007 (or later) to edit the object. Finally, if you create a custom recipient filter, you must use Exchange Management Shell to edit it, because Exchange Management Console can only deal with precanned filters.

It’s easy to use the shell to create a dynamic group to address all users with mailboxes on a server. When you use a dynamic DL for this purpose, the advantage is that you don’t need to maintain it as mailboxes are added, deleted, or moved around. Listing 5 shows the code to create a new dynamic group that addresses all the mailboxes on a server called London-Mbx-1. When a dynamic group has outlived its usefulness, you can remove it using the Remove-DynamicDistributionGroup command:

Remove-DynamicDistributionGroup `
   -id 'Mailboxes on London Server'

The Future of PowerShell and Exchange
PowerShell is undoubtedly a big part of the future for Exchange server administrators. And PowerShell’s inclusion in Windows Server 2008 indicates its growing importance in the Windows administration landscape. Microsoft has produced a complete set of commands to work with DGs in Exchange 2007, so the only question now is how and when you can take advantage of their work as you deepen your understanding of what Exchange Management Shell can do in your organization.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Accessing Database Data with ADO

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
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 Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing