前言

最近工作接觸到透過 Powershell 設定 Exchange 上的通訊清單 (Address list),透過 AD 同部分的方式,將符合條件的使用者加入在通訊清單內,方便企業用戶查詢使用。您可以在 Outlook 內選擇新增電子郵件 > 通訊錄 找到一般通訊錄。本篇文章簡單紀錄相關語法,若有錯誤或任何建議,請各位先進不吝提出,謝謝



操作流程

1. 在 Windows 內透過系統管理員 (Administrator) 開啟 Powershell


2. 若您在這台電腦是第一次使用 Powershell 連接 Exchange,請先透過下列指令進行設定
`Set-ExecutionPolicy RemoteSigned`


3. 你可以透過第一行指令,彈出視窗內輸入帳號密碼後,透過第二行指令建立連線
$UserCredential = Get-Credential 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirectio



若你不想透過彈出視窗進行驗證,你也可以透過下列指令建立連線 (請替換掉帳號與密碼,上下方法擇一即可)
$User = "your_account@xxxxx.onmicrosoft.com"
$Password = ConvertTo-SecureString -String "your_password" -AsPlainText -Force
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection


4. 輸入下列指令將 powershell 指令下載回來


5. 您可以透過下列指令取得通訊錄內容 (第三個條件為符合條件,若使用者資料符合條件,則會被同步歸類在此通訊錄)
Get-AddressList



6. 新增通訊錄語法 (您可以透過 第 5 個步驟 取得一般通訊錄確認是否新增成功)
New-AddressList -Name "TestAL" -RecipientFilter {((RecipientType -eq ' UserMailbox') -and (Department -like 'Test*') )}


7. 設定通訊錄語法
Set-AddressList -Identity"TestAL" -RecipientFilter {((RecipientType -eq ' UserMailbox') -and (Department -like 'Test123*') )}


8. 設定通訊錄語法 (不出現 confirm 訊息)
Remove-AddressList -Identity TestAL -Confirm:$False