Yesterday someone asked me to help create a distribution list for everyone reporting to a particular manager (directly or indirectly). Needless to say, that PowerShell makes getting a list of such user accounts a piece of cake!
Here’s the quick script (using AD cmdlets) which I emailed back:
function Get-QADIndirectReport { param ($Identity) # Find all direct reports Get-QADUser -Manager $Identity | ForEach-Object { # Output direct report $_ # Then recursively call this function for all # reports of this report Get-QADIndirectReport -Identity $_ } } # usage example Get-QADIndirectReport 'Dmitry Sotnikov'
Basically, AD cmdlets natively can retrieve all direct reports, and I have created a function which keeps going deeper level-by-level getting everyone reporting indirectly as well.
You can then take this a few steps further. For example, say, you want to get a list of users you could then just copy/paste into Outlook. Simply select the Email property from the user objects and ask PowerShell to put semicolon between the addresses:
# get a list of addresses for an email message (Get-QADIndirectReport 'Dmitry Sotnikov' | Select-Object -ExpandProperty Email) -join '; '
Or you could indeed use the list to populate a group:
# add everyone to a group Get-QADIndirectReport 'Dmitry Sotnikov' | Add-QADGroupMember DmitrysReports
Or you could further restrict the list by City, Department and so on by simply tweaking Get-QADUser parameters in the code above. PowerShell is super-flexible!
I guess you just found a use case for a -recurse or -rollup parameter in the Get-QADuser cmdlet.
Regards,
Klaus
I guess so… The question is – how common is really this particular use-case to implement it in the cmdlet code as opposed to this quick blog post?
I bet one could probably also create a proxy function, adding a -RecursiveManagedBy parameter…
This is good stuff!!
Any idea how i might generate a list of users that have direct reports?
For example: supervisors, managers, directors, VP’s, etc etc.
Dwayne,
Yep, see the script here: http://powergui.org/thread.jspa?threadID=16541
Dmitry
This is great! How do I “include” get-qadindirectreport in the Quest Powershell command prompt? When I run the script with the .\ in the Quest command line, I don’t seem to be able to invoke get-qadindirectreport. If I run the “PowerGUI script editor” and run it there, then I can… is there a way to get it to run natively in the Quest Activeroles Management shell?
Just add the Add-PSSnapin to your scripts or PowerShell profile.
Just a quick question, what if I wanted to add a report to this script that would document all the users that our added to the group? Is there an easy way to output the list of users to a file or something as they are added?