List DL with No Member and Send Email notification to DL owners.

The script gives list of DLs that has NO member. The script is NOT intended to give nested groups. The output of the script will give only DL name that has NO member   and sends mail to DL Owner.

$memGrpCnt=0
$mgrby="NotSet"

$grplst=get-distributiongroup -ResultSize unlimited

$grplst | foreach-object {

$curgrpmplt++

$curgrp=$_

$grplstls=$curgrp.alias
$mgrby = $curgrp.managedby

$dn= $curgrp.displayname

write-progress -activity "Checking the DL to calculate " -status "Currently processing the DL(alias) :     $grplstls" -percentcomplete (($curgrpmplt/

$grplst.count)*100)

$memGrpCnt=@(get-distributiongroupmember $curgrp.alias -ResultSize unlimited).count

if ($memGrpCnt -eq 0)  {

if ($mgrby -ne $null) {

$t = $mgrby
$t | foreach {

$Mby = $_.Name
$MbyMail = get-recipient $Mby | select PrimarySmtpAddress |ft -HideTableHeaders | out-string
write-host $dn,$memGrpCnt,($MbyMail).trim()
"$dn,$memGrpCnt",($MbyMail).trim() | out-file -filepath C:\test\DLwithGrp.txt -append

$tempmsg= [string] (get-content ("c:\NoMEMMSG.htm"))

$tempmsg = $tempmsg -replace "NewUser00",$Mby
$tempmsg = $tempmsg -replace "DL00",$dn

send-mailmessage -from administrator1@exchange2010.com -to ($MbyMail).trim() -subject "No member DL !!!" -body $tempmsg  -BodyAsHtml -SmtpServer

"192.168.0.41"

}
}
else {
$Mby = "Unknown"

}

$memGrpCnt=0

}

else  {  $memGrpCnt=0  }

}
write-host "The Script completed" -foregroundcolor "Green"

 

 

 What the script does,

- It takes all the DLs and stores in $grplst.

- Then loops thru each DL to check member count

- If member count equal to zero, script picks the owners and sends email.

- All the owners will receive email.

- Also script updates the text file with DL with zero count member and email address of owner.

- The script uses html file to email

- You may receive error :  5.7.1 Client was not authenticated while sending error, while running script. This can be avoided by creating new receive connector and set the Anonymous Users box checked under Permission Groups and also do not foget to add IP address of the machine from which the script runs. If the option does not sound good, pls use get-credential option.

- Please make the changes in below location in the script to your need,

   > "$dn,$memGrpCnt",($MbyMail).trim() | out-file -filepath C:\test\DLwithGrp.txt -append

   > $tempmsg= [string] (get-content ("c:\NoMEMMSG.htm") )

   > send-mailmessage -from administrator1@exchange2010.com-to ($MbyMail).trim() -subject "No member DL !!!" -body $tempmsg  -BodyAsHtml-SmtpServer

"192.168.0.41"

- NewUser00 and DL00 in html file are place holder.

- The scripted tested with Exchange 2010 and Windows 2008 R2.

You can download the script from : https://gallery.technet.microsoft.com/office/Script-to-List-DL-with-No-5db0c44f