Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

Microsoft Exchange – Set address book policy based on group membership

PowerShell Automating Administration

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

Related Posts

Leave a comment

You must be logged in to post a comment.