Manager and direct reports

Hi,

I am trying to create a dynamic distribution group in office 365, where I can create membership based on manager “ms” his reportees aging his reportees have reportees till last level.

I am still getting error in the recipientfilterscript block, can you validate and help me to understand



Hi,

I am trying to create a dynamic distribution group in office 365, where I can create membership based on manager "ms" his reportees aging his reportees have reportees till last level.

I am still getting error in the recipientfilterscript block, can you validate and help me to understand


$ManagerSAM = “MS”

function Get-AllReportees {
param (
[Parameter(Mandatory = $true)]
[string] $ManagerSAM
)

$directReports = Get-ADUser -Filter { Manager -eq $ManagerSAM } -Properties Manager
$allReportees = @()

foreach ($reportee in $directReports) {
    if ($reportee.Name -ne $ManagerSAM -and $reportee.Manager -and $reportee.Manager -is [Microsoft.ActiveDirectory.Management.ADUser]) {
        $childReports = Get-AllReportees -ManagerSAM $reportee.Manager.SamAccountName
        $allReportees += $childReports
    }
}

return $allReportees

}

$allMembers = Get-AllReportees -ManagerSAM $ManagerSAM

try {
$recipientFilterScript = {
$args[0].SamAccountName -eq $ManagerSAM -or $allMembers.SamAccountName -contains $args[0].SamAccountName
}

New-DynamicDistributionGroup -Name "DDG-MGR-MS" -RecipientFilter $recipientFilterScript
Write-Host "Dynamic Distribution group: DDG-MGR-MS Successfully created"

} catch {
Write-Error $_.Exception.Message
}

Welcome back!
When you have a moment, please go back and format your code: Formatting your code: How to format code on PowerShell.org

It seems like some of it formatted and some of it isn’t. Sometimes, funky things can happen if all the code isn’t formatted as code. Much appreciated!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.