豆子今天遇见个小问题,发现某个Office365的邮件组的成员组里面居然没有配置邮件,这样导致个别用户没有收到邮件。为了避免这个情况再次发生,需要对所有的邮件组都做个检查。问题在于邮件组可能嵌套了多个组,如果人工去看实在太累,写了个小脚本扫一下。
因为是嵌套的组,于是很自然的想到了递归。指定一个邮件组,去扫一下成员,看看该成员是否配置了邮箱地址,如果这个成员刚好又是一个组,那么调用自己,重复上述步骤
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
{
}
}
简单测试一下我的函数,结果如下: 普通用户(×××),绑定了邮件的组(蓝色),没有绑定邮件的组(红色)

成功。