Get the App
SLTechnology News&Howtos  ›  Servers  › 

PowerShell recursive query group members

Shulou Source: shulou.com Published: 2022-06-02 17:34:25 09月12日 Update

Douzi encountered a small problem today when he found that there was no email configured in the member group of an Office365 mail group, which led to individual users not receiving email. To prevent this from happening again, all mail groups need to be checked. The problem is that the mail group may have multiple groups nested. If you are too tired to read it manually, write a small script to scan it.

Because it is a nested group, it is natural to think of recursion. Specify a mail group and scan the member to see if the member is configured with an email address. If the member happens to be another group, call yourself and repeat the above steps

Function get-member {[CmdletBinding ()] [Alias ()] [OutputType ([int])] Param (# Param1 help description [Parameter (Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true) Position=0)] [string] $name) Begin {} Process {$a=Get-DistributionGroupMember $name-ErrorAction SilentlyContinue if ($a-eq $null) {return} foreach ($b in $a) {if (($b.Recipienttype-eq'Usermailbox')-or ($b.Recipienttype-eq' MailContact')- Or ($b.Recipienttype-eq 'User') {write-host $b.name-ForegroundColor DarkYellow} else {if ($b.primarysmtpaddress-eq "") {write-host $b.name-ForegroundColor red} else { Write-host $b.name-ForegroundColor Cyan get-member $b.name}} End {}}

Briefly test my function, and the results are as follows: ordinary user (× × ×), group bound with mail (blue), group without bound message (red)

Success.

Tags: Mail member user question configuration recursion normal successful manual again function again address multiple case step red result script natural Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Huawei Shulou Technology Xiaomi