PowerShell How-To

Working with Recursive Functions in PowerShell

Recursive functions are designed perfectly to manage what could be a hierarchical mess in Active Directory.

When working with Active Directory groups, a common requirement is to enumerate all members of several different groups. This is done to typically figure out what kind of permissions each user in that group has. However, an administrator soon realizes that just checking group membership is not enough since user accounts are not the only object inside of groups but other groups can be members as well. This means that in order to get an accurate representation of all of the groups a user account is in, she must also enumerate those nested groups as well.

This is one of the most common examples of the need for recursive functions. Recursive functions, at their simplest, are functions that call themselves. These types of functions are used in the example above to enumerate a hierarchical structure like AD groups inside of other AD groups with user accounts inside of those. A recursive function is an excellent way to walk through each of these objects.

Using the example provided above, let's get to coding. Let's first develop a small script that queries for all user accounts in a group. This is an easy, one-liner for the Get-AdGroupMember command.

Get-ADGroupMember  -Identity 'Domain Admins' | Select-Object name,objectClass
Figure 1.

You can see that my Domain Admins group has a user and a computer account inside. Let's now make things interesting and nest a group inside of Domain Admins. I'll add my IT Admins group since I need all users in that group to be a domain admin and query the group membership again.

Get-ADGroupMember -Identity 'Domain Admins' | Select-Object  name,objectClass
Figure 2.

If I'm building a report to investigate who has domain admin rights in the domain, I now don't have a complete list. Since all nested groups inherit permissions from parent groups, this means that all users inside of the IT Admins group are now members of the Domain Admins group. How can I modify my code to do this? One way would be first to detect if the object inside of the group is another group and, if so, then enumerate that group.

$daMembers = Get-ADGroupMember -Identity 'Domain Admins' |  Select-Object name,objectClass
$daMembers | foreach {  if ($_.objectClass  -eq 'group') { Get-AdGroupMember -Identity $_.name } else { $_ } }

Below you can see that jschmoe and bfischer are members of the IT Admins group.

Figure 3.

This would work but what if there's another nested group inside of the IT Admins group? Then another group inside of that, and another group inside of that.... You get my point. By trying to use your previous strategy of just calling Get-ADGroupMember again could turn into something like this. Then would only work if no one nests another group inside of the smallest child! This isn't sustainable.

$daMembers = Get-ADGroupMember -Identity 'Domain Admins'
$daMembers | foreach { 
if ($_.objectClass  -eq 'group') {
Get-AdGroupMember -Identity $_.name | foreach {
if ($_.objectClass  -eq 'group') {
Get-AdGroupMember -Identity $_.name | foreach {
...........
}
} else {
$_
}
}
}
} else {
$_
}
}

Instead, let's build a recursive function around this so that it can call upon itself if it detects a group. This way it doesn't matter how many nested groups exist in this chain. It will just recognize the group, enumerate the members and continue on.

function  Get-DomainAdminUserRights {
param (
[Parameter()]
[string]$GroupName = 'Domain Admins'
)

    $daMembers = Get-ADGroupMember -Identity $GroupName
$daMembers | foreach { 
if ($_.objectClass  -eq 'group') {
## Run the function again which will check for the group, call the function
## again, if necessary and output the non-group objects
Get-DomainAdminUserRights -GroupName $_.name
} else {
## Send the non-group object out
[pscustomobject]@{
Name = $_.Name
FromGroup = $GroupName
}
}
}
}

I can now run this function and get the exact same result as last time but this time I've added a little code to see what group each user is from. The only difference now is that I can nest as many groups as I want without changing a line of code!

Get-DomainAdminRights
Figure 4.

Notice now that by just querying for membership of the Domain Admins group that somehow the Accounting groups is in that chain! Ouch!

This is an excellent example of recursive functions. As you write more PowerShell scripts, there will be times when recursive functions come in handy. Recursive functions work best when working with hierarchical relationships like this. These functions allow you to write code once and, no matter what's changed in the environment, no further changes will be necessary for your code.

About the Author

Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.