How to compare/check two attributes – Mail and PrimarySmtpAddress – and create Excel report if they are not the same.
Edit line 6 if your DistributionGroups have different name convention.
You will find Excel file on your Desktop under the name: Compared-DG-Attributes.xlsx. If you want to change that, edit line 4.
Note:
Import-Excel module is necessary for this.
Clear-Host
# EXPORT LOCATION
$export = "$env:USERPROFILE\Desktop\Compared-DG-Attributes.xlsx"
# LIST OF DISTRIBUTION GROUPS
$DGlist = Get-DistributionGroup -Filter 'Name -like "DL*"' -ResultSize Unlimited
$data = foreach ($g in $DGlist) {
Write-Progress -Activity "Working on: $($g.Name)"
$dgName = $($g.Name)
$mailAtt = (Get-ADGroup -Identity $g.Name -Properties * | Select-Object Mail).mail
$psmtp = $($g.PrimarySmtpAddress.Address)
# COMPARE ATTRIBUTES
If ($psmtp -eq $mailAtt) {
# Nothing to do here..
}
Else {
# Here we will catch the object and save it to our custom object.
# CREATE CUSTOM OBJECT WITH NEEDED DATA
[PSCustomObject]@{
Name = $dgName
Mail = $mailAtt
SMTP = $psmtp
}
}
}
# EXPORT DATA TO EXCEL
$data | Export-Excel -path $export -FreezeTopRow -BoldTopRow -AutoSize
Write-Host "Script finished. You will find exported file on your desktop."