#region IsIn 判斷某個用戶是否在郵件組中 Function IsIn([string]$User,[string] $GroupName){ if($User -eq "" -or $GroupName -eq ""){ Write-Warning "參數丟失,正確的格式爲 IsIn userName GroupName" break } Write-Host "【Start】 Search $user in ("$GroupName ") group" $Result = Get-DistributionGroupMember -ResultSize unlimited $GroupName | where {$_.name -eq $User -or $_.PrimarySmtpAddress -eq $user} if($Result -eq $null){ $SubGroups = Get-DistributionGroupMember -ResultSize unlimited $GroupName | where {($_.RecipientType -eq "MailUniversalDistributionGroup") -or ($_.RecipientType -eq "MailUniversalSecurityGroup")} | select name if($SubGroups -ne $null){ ForEach($SubGroup in $SubGroups){ IsIn $user $SubGroup.name } }else{ Write-Host -fore red "$user is not in group ("$GroupName ")," $result=Get-Mailbox -Identity $User -errorAction silentlyContinue if($result -eq $null){ Write-Warning "The email account $user is not exist." break } } } else{ Write-Host $User "is found in group" $GroupName -foregroundcolor green } } #Example IsIn "用戶郵箱" "郵件組名稱" #endregion