﻿# 已啟用 MFA 的 Office 365 組織帳戶如何連線至 Exchange Online PowerShell

[之前](/post/2019/12/28/office-365-outlook-mfa)參考過 [使用多重要素驗證連線至 Exchange Online PowerShell](https://learn.microsoft.com/zh-tw/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?redirectedfrom=MSDN&view=exchange-ps) 文件，但是步驟非常雷，除了一定要用【IE】進入 Exchange 系統管理中心 (EAC) 外，這份文件使用傳統的 [ClickOnce](https://learn.microsoft.com/zh-tw/visualstudio/deployment/walkthrough-creating-a-custom-installer-for-a-clickonce-application?view=vs-2019) 安裝方法不知為何沒辦法在我的 Windows Server 2016 成功安裝。本篇文章介紹另一種單純用 PowerShell 連線的方式，也就是使用新版的 Exchange Online PowerShell V2 模組來管理 Exchange Online。

### 問題描述

我公司的某一台 Windows Server 2016 透過 IE 連入 **Exchange 系統管理中心 / 混合** 之後，點擊 **Exchange Online PowerShell 模組支援多重要素驗證** 下方的 \[設定\] 會遇到完全沒反應的情況：

![Exchange 系統管理中心 / 混合 / 安裝 / Exchange Online PowerShell 模組支援多重要素驗證。下載該模組以使用更安全的方式管理 Exchange Online。](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/0115b2b82a9111999ecb-73465774-d766b800-43bb-11ea-9527-a6b2b14bb651.jpg)

其實 ClickOnce 看起來有運作，但不知道為何應用程式就是無法執行。我透過另外一台 Windows 10 下載了這個 `Microsoft.Online.CSE.PSModule.Client.application` 應用程式下來，複製到我的 Windows Server 2016 之後，依然無法執行，非常奇怪。目前我還是找不到問題發生的原因。

### 使用純 PowerShell 的連線方式

1.  以系統管理員身分開啟 PowerShell
    
2.  安裝 PowershellGet 模組
    
    ```powershell
    Install-Module PowershellGet -Force
    ```
    
3.  更新 PowershellGet 模組
    
    ```powershell
    Update-Module PowershellGet
    ```
    
4.  設定執行原則
    
    ```powershell
    Set-ExecutionPolicy RemoteSigned
    ```
    
5.  安裝 Exchange Online PowerShell V2 (EXO V2) 模組
    
    ```powershell
    Install-Module -Name ExchangeOnlineManagement -Force
    ```
    
6.  匯入 Exchange Online PowerShell V2 (EXO V2) 模組
    
    ```powershell
    Import-Module ExchangeOnlineManagement
    Get-Module ExchangeOnlineManagemen
    ```
    
    > 我目前的最新版本是 `0.3555.1`
    
7.  此時用以下命令連接 Exchange Online 就會自動跳出視窗提示 MFA 驗證並登入
    
    ```powershell
    Connect-ExchangeOnline -UserPrincipalName testuser@contoso.com -ShowProgress $true
    ```
    
    > 請將 `testuser@contoso.com` 更換成你的 Office 365 主要信箱地址 ([UPN](https://learn.microsoft.com/en-us/azure/active-directory/hybrid/plan-connect-userprincipalname#what-is-userprincipalname))
    
    如果帳號並沒有啟用 MFA 的話，需要改用以下命令登入：
    
    ```powershell
    $UserCredential = Get-Credential
    Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $true
    ```
    

### 相關連結

-   [使用 Exchange Online PowerShell V2 (EXO V2) 模組](https://learn.microsoft.com/zh-tw/powershell/exchange/exchange-online/exchange-online-powershell-v2/exchange-online-powershell-v2?view=exchange-ps)
-   [Azure AD UserPrincipalName population](https://learn.microsoft.com/en-us/azure/active-directory/hybrid/plan-connect-userprincipalname)
-   [使用多重要素驗證連線至 Exchange Online PowerShell](https://learn.microsoft.com/zh-tw/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?redirectedfrom=MSDN&view=exchange-ps)
