Active Directory Manager Security Group Review
We currently have a very antiquated solution to something that I thought would be somewhat common, but doesn't seem to be. My PowerShell skills don't quite seem up to the task.
The basic premise is to create reports for each manager so that they can easily see which security (and distribution) groups their members are a part of, and reply back in email form which groups they should not be in. I know that there are canned solutions out there for this, but at the moment we lack the resources and are looking to replace an in-house solution. My thought was to redo it in PowerShell. The report would look something like this:
| Report for ManagerA | ||
|---|---|---|
| SecurityGroup1 | SecurityGroup1 Description | |
| User1 | ||
| User2 | ||
| User3 | ||
| SecurityGroup2 | SecurityGroup2 Description | |
| User1 | ||
| User3 | ||
| User4 |
What I have so far: I begin by getting the direct reports for a given manager. Eventually I will code to pull all managers and automatically create separate reports for each of them, but I constrained it to one for testing. I also pull which groups each of those direct reports are in.
$manager = "Manager1"
$Users = Get-ADUser -Identity $manager -Properties directreports | Select-Object -ExpandProperty directreports | Get-ADUser -Properties Name, MemberOf, Enabled | ? {$_.Enabled -eq $true} | Sort Name
Then I loop through those users and create a new PSCustomObject named $obj. It will have a separate value for each security group it finds. I use a Try/Catch block to test whether the group is already listed. If not, it adds the group and the user that is a member. If it exists, then it just adds the user. I know this is not optimal and I know I don't really have a clue how to use a PSCustomObject.
Foreach ($User in $Users) {
ForEach ($Group in $User.MemberOf) {
# Get name and description
$GroupProp = Get-ADGroup $Group -properties name, description
Try {
# Try adding security group to list
$obj | Add-Member -MemberType NoteProperty -Name $GroupProp.name -Value @($User.Name) -ErrorAction Stop
} Catch {
# Already contains security group, add user name
$obj.$($GroupProp.Name) += @($User.Name)
}
}
}
At this point, I have an object that is essentially a listing of security groups that direct reports of a specified manager is a member of. And under that is a list of names that appear in those groups. I'm just not really sure where to go from here.
I had a few other ideas on how to approach this. I know I could just loop through every group in Active Directory and use filters to only show groups and members that report to that manager. That would probably be the easiest. I'd just write the report as I go to html and then convert it to pdf, but it would be more resource intensive, and not the way it should be done.
Am I completely overthinking this? Is there a better way? Or am I on the right track creating my own nested object?
Posted Dec 11, 2018, 8:24 PM UTC
Something like this should get you 90% there... the exact formatting as you had it isn't too easy, you'll have to basically manually create it:
There might also be an easier way to get this... but it works for me.
Edit: Yikes I just realized this is technically all one line... really shows the power of the PowerShell pipeline...
It's a thing of beauty.
That's great and all in one line... awesome! I've got it outputting to an html file now and this is a great start. I'll probably loop through it and change the formatting. I have one question though... I've tried adding the group description but it isn't pulling it for some reason. I tried to keep the code in line with yours:
Sample Output
https://i.imgur.com/QsfHafq.png
Group-Object outputs a [groupinfo] object. You can get an idea of the structure with this test:
So, to get the desc, you've got to dig deeper:
The [0] here is because the desc property will obviously be identical for every member.
Now, if you are to save the whole structure to use in a report, I'd suggest to remove the -join and keep the users list as an array.
That's perfect! Thanks for helping me understand it. I was thinking about removing the join so I could output the users on a new line too. Working on that now.
It's weird, but it can be done. On my small example, you'd put everything on two columns like this:
Note that you have to name the columns for that. Other ways of doing it would be to output text (but you'd have to account for the strings lengths yourself), to output text wrapped manually in a HTML table, or to export the whole thing in a CSV without header.