Exchange

Microsoft Exchange – Set address book policy based on group membership

You may remember our older post how to set address book policy based on group membership. After using this method above for a while we've found there is a far easier solution to get group membership with a recursive option on newer systems. We've also updated setting address book policy method with few more checks so it's a bit more error proof.

Solution

So instead of code from above link you can simply use this one

$global:logPlace = "C:\ExchangeScripts\Automated\ExchangeService.log"

function Global-LogInfo ([string] $logFile, [string] $msg, [string] $color = "Blue", [string] $backgroundColor = "White", [boolean] $showOnly = $false) { 
   $timeFormat = "yyyy-MM-dd HH:mm:ss" # https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
   if ($showOnly -eq $true) {
        Write-Host "[$([datetime]::Now.ToString($timeFormat))]$msg" -ForegroundColor $color # -BackgroundColor $backgroundColor
   } else {
        Write-Host "[$([datetime]::Now.ToString($timeFormat))]$msg" -ForegroundColor $color # -BackgroundColor $backgroundColor
        if ($logFile -eq $null) {

        } else {
            Write-Output "[$([datetime]::Now.ToString($timeFormat))]$msg" | Out-File $logFile -Encoding utf8 -Append
        }
  }
}
function Get-GroupMembershipRecursive ($group) {
   $AllMembers = Get-ADGroupMember $group -Recursive | Get-ADUser -Properties DisplayName, Mail,msExchHomeServerName -ErrorAction SilentlyContinue | Select Name,msExchHomeServerName,ObjectClass,Mail, DisplayName
   Write-Output $AllMembers
}
function SetAddressBookPolicy ([string] $AddressBookPolicy, [string] $SetOnGroup, [Boolean] $debug = $false) {    
    $members = Get-GroupMembershipRecursive $SetOnGroup
    foreach ($member in $members) {
        #Global-LogInfo -logFile $global:logPlace -msg "[+] Information: $($member.Name) with email $($member.Mail) [+]"            
        if ($($member.Mail) -ne $null -and $($member.msExchHomeServerName) -ne $null) {
            $mailbox = Get-Mailbox $member.Mail | Select AddressBookPolicy 
            if ($($mailbox.AddressBookPolicy)-eq $AddressBookPolicy) {
                if ($debug -eq $true) {
                    Global-LogInfo -logFile $global:logPlace -msg "[+] Already set $AddressBookPolicy on $($member.Name) address. Skipping. [+]"
                }
            } else {
                
                Global-LogInfo -logFile $global:logPlace -msg "[+] Setting $AddressBookPolicy on $($member.Mail) address. Replaced address book policy: $($mailbox.AddressBookPolicy) [+]"               
                Set-Mailbox $member.Mail -AddressBookPolicy $AddressBookPolicy
            }
        } else {
            if ($debug -eq $true) {
                Global-LogInfo -logFile $global:logPlace -msg "[+] Skipping $AddressBookPolicy on $($member.Name) address. [+]"
            }
        }
    }

}

Remember to use following commands to make sure the Global Address List, Address Lists and Offline Address Book are all up to date.

Get-GlobalAddressList | Update-GlobalAddressList
Get-AddressList | Update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook

This post was last modified on November 10, 2018 12:17

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

5 days ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

4 months ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

8 months ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

1 year ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 year ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 year ago