Powershell: 1: Query highest ranking member in AD security group.2. Find highest ranking member's AD management hierarchy.

PKI John 1 Reputation point
2022-06-15T17:55:20.483+00:00

Powershell 5.1
AD module install

I would like to send out some email notifications to the highest-ranking member in an AD security group. I also want to query the AD management hierarchy and send an email notification to their managers.

Thanks,

John

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-06-15T18:38:07.887+00:00

    What determines the "rank" of a group member?

    What determines the "AD management" hierarchy?

    0 comments No comments

  2. PKI John 1 Reputation point
    2022-06-16T20:44:20.36+00:00

    The possible ranks are: Engineering Lead, Manager, Director, and Executive Director and they would be populated in the "job title" field in AD for the user.

    The AD manager is populated in the Use's AD profile, field: Manager


  3. Rich Matheisen 47,901 Reputation points
    2022-06-22T02:07:10.807+00:00

    I haven't run this, but see if it gives you a way to get the information you want to send the e-mail:

    $Rank = @{  
        "Engineering Lead" = 3   
        Manager = 2  
        Director = 1  
        "Executive Director" = 0  
    }  
      
    $Ranking = @{}          # will hold all the users with the job titles in $Rank and the their managers  
      
    $Group = "MyGroup"      # must be usable as an "Identity"  
      
      
      
    Get-ADGroupMember -Identity $Group |  
        Where-Object {objectCategory -eq 'user'} |                              # distinguish between user and computer (both are "user" objectClass)  
            ForEach-Object{  
                $u = Get-ADUser -Identity $_.distinguishedName  
                if ($Rank.ContainsKey($u.Title)){                               # is this user's job title one of interest?  
                    if ($Ranking.ContainsKey($Rank.($u.Title))){                # has another user been found with the same ranking?  
                        $h = [PSCustomObject]@{                                 # add whatever properties you want to the PSCustomObject  
                                Name    = $u.distinguishedName  
                                Mgr     = $u.manager  
                        }  
                        $Ranking.($Rank.($u.Title)) += $h                       # add user's DN and manager  
                    }  
                    else{                                                       # first title of this rank has been found  
                    $h = [PSCustomObject]@{                                     # add whatever properties you want to the PSCustomObject  
                            Name    = $u.distinguishedName  
                            Mgr     = $u.manager  
                    }  
                        $Ranking.($Rank.($u.Title)) = ,$h                        # use the "magic comma" to create an array of user/manager DNs  
                    }  
                }     
            }  
    $Ranking.Keys |  
            Sort-Object |  
                Select-Object -First 1 |  
                    $NamesAndManagers = $Ranking[$_]                            # a array PSCustomObject user and manager DNs for the highest ranking user(s) in the group  
      
    # Use $NamesAndManagers to get the managers e-mail address  
    # and send them the mail  
    # Be careful, becasue the highest ranking user may not have a manager!  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.