Compare Email Policy – Exchange

Sharing a Script that you can change according to your requirements.

We have used this to find out user mailboxes that doesn’t comply with the email policy.

For example: FirstName.LastName@domain.com

In first part of the script define the domain & get mailboxes with employeeid (Example if in your env if you have stored Empid in Cutomattribute12)

#######format Date################

$date = get-date -format d

$date = $date.ToString().Replace(“/”, “-”)
$output = “.\” + “EmlcomparisonReport_” + $date + “_.csv”

$Collection = @()

$domain = “domain”  #########define domain

$regex = “^[0-9]*$” ######define regex to find numbers– employeeid

####################
$allmbx = Get-Mailbox -resultsize unlimited | where{$_.CustomAttribute12 -match $regex} ### get user mailboxes
$allmbx | foreach-object {

$firstN = $null

$lastN = $null

$qd = get-qaduser $_.samaccountname

$firstN = $qd.FirstName

$lastN = $qd.LastName

$firstN = $firstN.trim()

$firstN = $firstN -replace ” “,””

$lastN = $lastN.trim()

$lastN = $lastN -replace ” “,””

$Emlpol=$firstN + “.” + $lastN + “@” + $domain + “.” + “com” ### email policy ########form email policy

############################################

Now script will just compare the email policy with actual primary smtp address assigned to user mailboxes.

If match is not found than it will get reported in the csv as output.(in the same location from which script is run)

Note: Quest Shell & Exchange management shell is required to run this script.

Download the script:

https://gallery.technet.microsoft.com/scriptcenter/Compare-Email-Policy-f6a158ed

############################################################################ 
#           Author: Vikas Sukhija 
#           Date: 12/08/2015 
#        Update: 
#        Reviewed: 
#           Description: Compare Email Policy Exchange 
############################################################################# 
# Add Exchange Shell... 
 
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $null) 
{ Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010} 
 
# Add quest shell 
If ((Get-PSSnapin | where {$_.Name -match "Quest.ActiveRoles.ADManagement"}) -eq $null) 
{ 
    Add-PSSnapin Quest.ActiveRoles.ADManagement 
} 
 
#######format Date################ 
 
$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
 
$output = ".\" + "EmlcomparisonReport_" + $date + "_.csv" 
$Collection = @() 
$domain = "domain" 
$regex = "^[0-9]*$" 
  
####################### 
 
$allmbx = Get-Mailbox -resultsize unlimited | where{$_.CustomAttribute12 -match $regex} 
 
$allmbx | foreach-object { 
$firstN = $null 
$lastN = $null 
  
$qd = get-qaduser $_.samaccountname 
$firstN = $qd.FirstName 
$lastN = $qd.LastName 
$firstN = $firstN.trim() 
$firstN = $firstN -replace " ","" 
$lastN = $lastN.trim() 
$lastN = $lastN -replace " ","" 
 
$Emlpol=$firstN + "." + $lastN + "@" + $domain + "." + "com" ### email policy 
 
#Write-host "Formed email policy address $Emlpol" -foregroundcolor Green 
 
$email = $_.PrimarySmtpAddress 
 
$mbx = ""| select FirstName, Lastname,SamaccountName,Email,Formedaddress 
#########compare email policy now 
 
if($email -eq $Emlpol){  
write-host "email addreses $email is as per email policy $Emlpol" -foregroundcolor Green 
} 
else{ 
write-host "email addreses is $email not as per email policy $Emlpol" -foregroundcolor magenta 
$mbx.FirstName = $firstN 
$mbx.LastName = $lastN 
$mbx.SamaccountName = $_.samaccountname 
$mbx.Email = $email 
$mbx.Formedaddress = $Emlpol 
$collection +=$mbx 
} 
 
} 
 
#export the collection to csv , change the path accordingly 
 
$Collection | export-csv $output -notypeinformation 
 
###############################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s