Good evening everyone! After several weeks of dodging postings of an applied nature I figured that it might be time to do a PowerShell focused posting. I like PowerShell…
Anyhow, I recently was doing something at a client site and I wanted to find groups that contained a certain string pattern in their name. If I were searching for groups in a single domain, this would be a very simple chore. However, I wanted to find groups that matched the pattern in all domains within the forest. Technically, I could search the entire forest using the [ADSI] type adapter. However, to go get the objects from all the domains in the forest would have required me to create a DirectorySearcher object for each domain, and then search that domain.
In other words, doable… but more code then I wanted to type. So… I instead decided to use the Quest AD cmdlets and came up with the following:
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain.Forest.Domains | foreach {Get-QADObject -Service "$($_.name):389" -LdapFilter "(&(objectClass=group)(cn=*Pattern123*))"} | ft Name,Description
Following the two lines of code, I first create a $Domain object using the GetCurrentDomain method of the DirectoryServices.ActiveDirectory.Domain class. Then using this $Domain object, I get a list of all of the domains in the forest. Next, I loop through that list of domain and search for the groups matching my defined pattern using the Get-QADObject.
Simple...
If you like this, check out some other posts from Tyson:
- When a computer science degree matters, and when it doesn't
- Since when did cloud computing become/need a manifesto?
- Why would one phish using a Certificate Authority (CA) as bait?
- Would I trust you, if everyone else trusted you?
- Here is a good question: Is scripting programming or just systems administration?
- PowerShell boy and the case of the missing cmdlets!
- Fun with PowerShell 2.0 Eventing!
- Creating a custom 404 page to handle link redirection for ASP.NET web applications
Or if you want, you can also check out some of Tyson's latest publications:
- Windows PowerShell Unleashed (2ndEdition)
- Windows Server 2008 Unleashed (Yes, I did help on this book)
Lastly, visit the Microsoft Subnet for more news, blogs, and opinions from around the Internet. Or, sign up for the bi-weekly Microsoft newsletter. (Click on News/Microsoft News Alert)