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 


April 15, 2008

More About Using PowerShell to Manage Exchange Server 2003

Construct queries using information gathered directly from AD
RSS
View this exclusive article with VIP access -- click here to join | See More Scripting Articles Here | Reprints
Or sign up for our VIP Monthly Pass!

Download the Code Here

Executive Summary:

Use Microsoft Windows PowerShell scripting commands to query Active Directory (AD) to learn more about your Microsoft Exchange Server 2003 organization.

In “Manage Exchange Server 2003 Using Windows PowerShell and WMI”
(http://www.exchangeprovip.com/Article/ArticleID/97261/97261.html), I explained how to exploit the strengths of PowerShell and Windows Management Instrumentation (WMI) to manage Exchange Server 2003. I also discussed how to assemble the interactive commands into a PowerShell script (*.ps1) and run the resulting script securely. In this article, I show you how to use PowerShell to query Active Directory (AD) to learn more about your Exchange organization.

Exchange and Active Directory
In “Manage Exchange Server 2003 Using Windows PowerShell and WMI,” we built the following WMI Query Language (WQL) statement, which includes a where clause:

 $exmb = Get-WmiObject 
-namespace root\microsoftexchangev2
-query "select * from
Exchange_mailbox where
(servername='exchangeserver01') and
(storagegroupname='First Storage Group')"

The purpose of the where clause is to filter the query so as to limit the search scope based on one or more criteria, thus optimizing the process. Only objects that satisfy the criteria will be returned to the machine executing the query itself. In order for this to function, you must already know the correct names to use to check against the criteria (e.g., exchangeserver01, First Storage Group), or you must have maintained an accurate record of them in the Exchange organization.

Exchange 2003 relies heavily on a stable and functional AD infrastructure to operate properly. The majority of an Exchange organization’s configuration settings are stored in the Enterprise Configuration partition inside AD, more commonly referred to as the Configuration Naming Context (NC). You can find configuration information, such as Exchange connectors, administrative and routing groups, storage group, and Information Store, on a per server level. For more information, see the Microsoft Exchange Server 2003 Technical Documentation Library at http://technet.microsoft.com/en-us/library/bb123872(EXCHG.65).aspx.

Rather than trying to identify the names of administrative groups, servers, storage groups, or database information stores by painstakingly clicking through Exchange System Manager (ESM) manually, you can probe AD for the most up-to-date information. Use the following PowerShell code to query AD for Exchange configuration information:

$root= New-Object System.DirectoryServices.DirectoryEntry("LDAP://RootDSE")
$cfgNC = [adsi]("LDAP://" + $root.Get("configurationNamingContext"))
$search = New-Object System.DirectoryServices.DirectorySearcher($cfgNC)
$search.filter = '(objectclass=msExchExchangeServer)'
$exServer = $search.FindAll()

The first line of code instantiates a directory object from the Microsoft .NET Framework class that goes by the name System.DirectoryServices.DirectoryEntry. The parameter in the parentheses indicates that the object instance should connect straight to the root of the AD domain. This parameter is optional and doesn’t affect the result.

Line 2 defines the AD partition that we want to focus our attention on. You identify this partition by obtaining a reference to the configuration naming context that’s prefixed with the “LDAP://” identifier. Casting the resulting string to [adsi] causes PowerShell to return an object bound to an Active Directory Service Interfaces (ADSI) instance, which you then assign to the $cfgNC variable. [adsi] is a shortcut (or type accelerator) for the .NET type [System.DirectoryServices.DirectoryEntry]; the shortcut is created to help reduce the amount of typing necessary. Thus, you can enter [adsi] rather than [System.DirectoryServices.DirectoryEntry]; both will give the same result. This shortcut, in addition to the underlying ADSI object adapter, is how PowerShell provisions AD support.

The real work of searching requires yet another object instance created from the System.DirectoryServices.DirectorySearcher class, as line 3 shows. You specify the search scope by passing $cfgNC as a parameter.

Rather than sorting through all the returned items that might not be relevant (not to mention incurring an unnecessary network load and performance hit), you can set up a filter to retrieve only items that match particular criteria, as line 4 shows. You’re working with Exchange servers in this case, so the objectclass attribute must be set to msExchExchangeServer. (For more information about objectclass attributes, see Alain Lissoir, Understanding WMI Scripting, Digital Press, 2003; and Leveraging WMI Scripting, Digital Press, 2003.)

Now you’re ready to perform the actual search with the FindAll method. Line 5 accomplishes this task and stores the result in the $exServer variable.

To add another search criteria based on the names of Exchange storage groups, you can create a new statement similar to the one in line 4 but point it to the correct objectclass, as the following code shows:

$search.filter = '(objectclass=msExchStorageGroup)'
$exStorageGroup = $search.FindAll()

You must call the FindAll method again because you’re working with a different objectclass type. Make sure that you capture the result in a new variable for later use. Table 1 contains a list of commonly used attributes.

Bringing It All Together
Now you can rework the original code to be much more flexible. Instead of hard-coding the parameters for Exchange server and storage groups, you can construct the query using information gathered directly from AD.

Listing 1 contains code for dynamically querying mailbox information. Let’s dissect the code to make it less intimidating.

The first two lines send $exServer to the ForEach-Object cmdlet and store a reference to the current object coming down the pipeline (identified by $_). These steps are done for easy reference, to avoid confusion and overwriting other, unrelated $_ commands referenced elsewhere. Lines 3 and 4 perform the same tricks for $exStorageGroup, which describes the storage groups in the Exchange organization.

In line 5, we cast the distinguishedname property to a string type before we can use the built-in split method of this particular [string] class. Because we need only specific text characters to build the complete query, we have to do a bit of work to extract each one of them from the distinguished name (DN). The DN is stored internally in the form CN=First Storage Group,CN=InformationStore,CN=CHZUMX01,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=swissitpro,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=swissitpro,DC=local.

When we find a match to the name of the server that’s queried in line 6, we construct a string to fully describe the WQL that will pull out information pertaining to mailboxes (line 9). The query statement is built with the criteria from the names of matching Exchange server and storage groups and recorded in the $wql variable.

In line 11, the $exmb variable is assigned the result from running the Get-WmiObject cmdlet. Next, you start harvesting mailbox data that matches the criteria specified by the query.

Although running the code in Listing 1 gives you the same results as running the original query, you don’t need prior knowledge of an Exchange organization’s logical setup to use the code in Listing 1. In addition, this code gives you the flexibility to work with the different objectclass types exposed in Exchange through AD. And you can easily expand the code to further map out the profile for your entire messaging environment.

More Objects Properties
Regardless of the size or complexity of a company, it’s common to find an Exchange organization set up with multiple servers dedicated to messaging. To determine how many servers an organization has, you can use the $exServer.count command to access the count property of the server object denoted by $exServer. Then, you would typically iterate through the objects by piping to the ForEach-Object cmdlet, as I discussed previously. A major advantage of this method is that you don’t have to explicitly maintain a separate counter to index into the objects. Because search results are stored in zero-based collections or arrays of objects in PowerShell, you can still access individual elements by using the syntax $exServer[i] (i.e., $exServer[0] for the first Exchange server, $exServer[1] for the second Exchange server, and so on).

Several specific properties (or attributes) are associated with an object that uniquely identify an Exchange server. You can use the object dot notation to work with either the path or properties attribute. The properties attribute is particularly important because it contains key object information, as the output in Figure 1 shows. To actually access an object property, you must preface it with the properties keyword, such as $exServer[0].properties.distinguishedname or $exServer[0].properties.cn.

Although you see mainly text output on your host’s display, don’t forget that you’re always dealing with objects in PowerShell. Each property is in fact an object in its own right—not just plain text characters. You can verify the real object type representing a property by piping it to the Get-Member cmdlet (or the alias gm). For instance,

$exServer[0].properties.whenchanged | Get-Member

reveals that the whenchanged property is of the type System.DateTime, as the output in Figure 2 shows. In addition, the object’s applicable methods and properties are also displayed.

You might wonder why this information is important. Sometimes it’s useful to know the exact object type to facilitate easy handling. The distinguishedname property is actually of the [string] type, but the object itself is embedded in the System.DirectoryServices.ResultPropertyValueCollection, which is a collection or array of objects. No split method exists for this array object type, and invocation will fail unless you perform an explicit cast back to the [string] type.

You’ve Got the Power
You can use PowerShell commands to query AD to learn more about your Exchange organization. Then, rather than hard-coding the parameters for Exchange server and storage groups, you can construct a query using information gathered directly from AD. The more you work with PowerShell, the more you’ll discover its potential to help you administer your messaging systems.

 

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
The Memory-Optimization Hoax

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

Q. How can I use the command line to obtain a list of all the applications installed on my computer?

...

Windows SBS, EBS 2008 Hit RC1, Home Server PP1 Ships

Find out how to download the RC1 versions of Windows SBS 2008 and EBS 2008, and discover what's new in the PP1 update to Windows Home Server. ...


Related Articles Manage Exchange Server 2003 Using Windows PowerShell and WMI

Setting Up Exchange Management Shell

Windows PowerShell Transforms Exchange Server 2007 Management

Coming Out of Your Shell

Exchange Server and Outlook Whitepapers Anonymizers – The Latest Threat to Your Web Security

Replay for Exchange: Enterprise Protection and an Affordable Price

ETX Driving Embedded I/O

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting 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.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Shortcut Guide to SQL Server Infrastructure Optimization
With right tools and techniques, you can have a top-performing SQL Server infrastructure without having to cram your data centers so that they're overflowing. Download this eBook to learn how.

WinConnections 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).

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Continuous Data Protection and Recovery for Exchange
Read this white paper to learn about Continuous Data Protection (CDP), Exchange 2007's local continuous replication and cluster continuous replication features.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Tips to Managing Messaging
Discover three fundamental mail and messaging management services - security, availability and control services - and how you can implement them in a Microsoft-centric mail and messaging environment.

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Solving PST Management Problems
In this white paper, read about the top PST issues and how to administer local/network PST files.

Bandwidth Monitoring Tool from SolarWinds
Identify largest bandwidth users in seconds. Get the free download now.

Transform Your Data Center at Brocade Conference 2008
Storage networking industry’s premier event at the MGM Grand, Las Vegas, September 22 - 24, 2008

Are You Litigation Ready?
Collecting and processing electronic data for e-discovery can be time-consuming and expose a business to significant legal risks. Get prepared with this free white paper

Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.

KVM over IP Solutions
Learn about a KVM over IP solution that is specifically designed to meet the needs of the distributed IT environment.
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
IT Library Technical Resources Directory Connected Home Windows Excavator 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