Saturday, July 27, 2013

Exchange 2010 and above: Get all DLs with external email addresses

 

Recently I have had a chance to write the below script to generate a report of all Exchange distribution lists with external email addresses. If you like to generate a report with same purpose, feel free to use my script. Enjoy. Open-mouthed smile

   1: <#
   2:     Name: DLMembers-External.ps1
   3:     Purpose: List all the DLs that has external users (contacts), 
   4:     Member Names and their external email address.
   5:     Written by: Anand Venkatachalapathy
   6:     Created on : 07/26/2013
   7: #>
   8: z
   9: $smtpDomainName = "company.com"
  10:  
  11: #Get all DLs in Exchange organization
  12: $DLs = Get-DistributionGroup -ResultSize unlimited
  13: "DLs with external email addresses" | Out-File -FilePath ".\DL.txt"
  14:  
  15: #Check each DL 
  16: foreach ($dl in $DLs)
  17: {
  18:     #Get the DL memberss list
  19:     "`n`nChecking $($dl.name)..."
  20:     $members = Get-DistributionGroupMember $dl.Name -ResultSize unlimited
  21:  
  22:     $i = 0
  23:     #Check each members's email address
  24:     foreach ($member in $members)
  25:     {
  26:         $email = ($member.primarysmtpaddress.tostring()).ToLower()
  27:         $name = $member.name
  28:         
  29:         #Get DL's Owner (ManagedBy)
  30:         $j = 0
  31:         foreach ($DLManager in $dl.ManagedBy)
  32:         {
  33:             if ($j -eq 0)
  34:             {
  35:                 $DLOwners = $DLManager.Name
  36:             }
  37:             else
  38:             {
  39:                 $DLOwners = $DLOwners = ", " + $DLManager.Name
  40:             }
  41:             ++$j
  42:         }
  43:  
  44:         #check members's email address
  45:         if ($member.PrimarySMTPAddress.domain -ne $null )
  46:         {
  47:             if ( (!($email.contains($smtpDomainName)))  )
  48:             {
  49:                 #found an external email address
  50:                 if ($i -eq 0) { 
  51:                     "`n`n`n" + $dl.Name + "`nOwner(s): " + $DLOwners | Out-File -FilePath ".\DL.txt" -Append 
  52:                     "Name`tEmail" | Out-File -FilePath ".\DL.txt" -Append
  53:                     "`n`n`n" + $dl.Name
  54:                 }
  55:                 $i++
  56:                 $member.Name + "`t" + $member.primarysmtpaddress.tostring()
  57:                 $member.Name  + "`t" + $member.primarysmtpaddress.tostring() | Out-File -FilePath ".\DL.txt" -Append
  58:             }
  59:         }
  60:     }
  61: }
  62:  
  63: # * * * * End of Script * * * * 

Download the PowerShell Script here: http://sdrv.ms/12UlTzU

No comments:

Post a Comment

Followers

hit counter