Netwrix 1Secure 提供跨数据和身份的统一可见性——免费试用14天,享有完全访问权限。开始免费试用

资源中心博客

连接到 Exchange Online PowerShell

连接到 Exchange Online PowerShell

Aug 25, 2025

Exchange Online PowerShell 可实现对 Microsoft 365 邮件环境的安全、自动化管理。使用启用现代身份验证和 MFA 的 EXO V2 模块,管理员可在规模化场景下管理邮箱、权限、报表以及迁移。基于证书和托管身份的选项支持自动化,同时诸如 RBAC、TLS 和会话管理等最佳实践可增强安全性和合规性。

Exchange Online PowerShell 入门

Exchange Online PowerShell 是一种命令行管理界面,用于在属于 Microsoft 365 的 Exchange Online 中管理和自动化任务。它允许管理员管理用户邮箱、配置组织设置,并通过脚本高效执行批量操作。以下是使用 PowerShell 进行 Exchange Online 管理的一些优势:

  • 编写脚本以自动化重复性任务,例如创建邮箱和分配权限。
  • 可批量管理对象,例如在一次执行中更新或修改多个用户帐户、邮箱或组;同时也可以通过 CSV 文件导出和导入数据。
  • 可从任何受支持的设备远程连接到 Exchange Online,并通过加密会话安全地执行管理任务。
  • 获取更高级的报表和审计。例如,可以以 CSV、Excel 或 HTML 格式生成自定义报表,并且你可以 audit mailbox access 通过精确筛选来审计管理操作。
  • 访问在 EAC Web 界面中未公开的隐藏或高级设置。

Exchange Online 邮箱审计快速参考指南

了解更多

连接到 Exchange Online PowerShell 的先决条件

要连接到 Exchange Online PowerShell,您需要满足特定的先决条件。

系统要求

  • 操作系统:Windows 10、Windows 11 或 Windows Server 2016/2019/2022
  • Windows PowerShell 5.1 或更高版本
  • .NET Framework 4.7.2 或更高版本

网络要求

  • 确保允许出站 HTTPS(TCP 443)流量
  • 必须启用 TLS 1.2
  • 具备互联网访问能力,并能够连接到 outlook.office365.com、login.microsoftonline.com 和 graph.microsoft.com

所需模块

  • Exchange Online Management 模块

身份验证要求

  • 对于 Microsoft Entra ID,该帐户应具备必要的权限,例如:
  • 全局管理员
  • Exchange 管理员
  • 启用 MFA 的帐户需要用于现代身份验证的 Exchange Online PowerShell 模块

所需权限

你的账户应具有适当的 RBAC(基于角色的访问控制)角色:

  • 组织管理
  • 收件人管理

如何连接到 Exchange Online PowerShell

连接到 Exchange Online PowerShell 的不同方法包括:

Connect-ExchangeOnline 模块(现代方法)

您可以使用 Exchange Online 管理模块(Exchange Online Management Module,EXO V2)连接到 Exchange Online。此方法适用于日常的管理与运维任务,推荐使用。它支持现代身份验证(OAuth)和 MFA。

Azure Cloud Shell(基于浏览器的连接)

此方法允许管理员无需在本地安装即可直接从 Azure 门户使用 Exchange Online PowerShell。只要有浏览器,任何设备都可以访问 Exchange Online PowerShell,并且其中包含预安装的模块和工具。此方法推荐给偏好通过浏览器进行管理、或在本地对 PowerShell 工具的访问受限的管理员。访问路径为:Azure Portal > Cloud Shell > PowerShell。

Exchange Online 远程 PowerShell(已弃用)

这种使用远程 PowerShell(WSMan 协议)连接到 Exchange Online PowerShell 的旧式方法已被弃用,不建议用于新的部署。不过,如果要使用不支持更新版 Exchange Online Management Module 的旧脚本或系统,则仍可采用此方法。该方法不支持 MFA。

使用服务主体(基于证书的身份验证)

该方法可通过服务主体账号和基于证书的身份验证,实现无人值守或基于脚本的管理。它适用于自动化、CI/CD 管道和后台流程。作为前提条件,需要:Azure AD 应用注册以及证书配置。

连接方式对比

Method

Best For

Supports MFA

Supports Automation

Connect-ExchangeOnline (EXO V2 Module)

Day-to-day admin tasks

Yes

Yes

Azure Cloud Shell

Quick browser access

Yes

No

Remote PowerShell (WSMan)

Legacy scripts

No

Yes

Service Principal (Certificate Auth)

Automation, CI/CD

No

Yes

了解现代身份验证及其优势

现代身份验证是一种身份管理方法,它利用 OAuth 2.0 以及 Active Directory Authentication Library(ADAL)或 Microsoft Authentication Library(MSAL)来实现安全登录。它取代了诸如基本身份验证(basic authentication)等传统身份验证方式。

现代身份验证是连接到 Exchange Online PowerShell 的推荐且安全的方式,因为它具有以下优势:

  • 它使用 OAuth 2.0 进行基于令牌的身份验证,而不是直接传递凭据,从而增强安全性。
  • 支持使用 MFA,从而增加额外的安全层。
  • 允许管理员根据设备合规性、位置或风险级别实施策略,这有助于根据已定义的条件限制对 Exchange Online 资源的访问。
  • 与 Microsoft Entra ID 和第三方身份提供商无缝集成,并支持单点登录 (SSO)。

Microsoft 将逐步弃用基本身份验证,因此迁移到现代身份验证至关重要。

使用或不使用 MFA 连接到 Exchange Online PowerShell 的要求

以下是使用或不使用 MFA 连接到 Exchange Online PowerShell 的要求。

Without MFA (Basic Authentication – Deprecated Legacy Method)

Microsoft Entra ID account must not have MFA enabledBasic authentication must still be allowed (if not blocked organization-wide)

With MFA (Modern Authentication)

The Microsoft Entra ID account must have MFA configuredOAuth 2.0 must be supported (default in Exchange Online)

连接步骤指南

以下是连接到 Exchange Online PowerShell 的简要步骤:

  1. 安装 Exchange Online PowerShell 模块。
  2. 使用 Connect-ExchangeOnline cmdlet 连接到 Exchange Online。系统提示时,输入你的 Microsoft 365 Exchange 管理员凭据。
  3. 连接到 Exchange Online 环境后,你可以使用 Exchange Online PowerShell 模块中提供的 cmdlet 来管理 Exchange Online 设置以及诸如邮箱、联系人和日历等对象。

安装 Exchange Online 管理模块

Exchange Online Management 模块可以安装到 Windows、Mac 和 Linux 系统中。以管理员身份打开 PowerShell,然后运行以下命令以安装最新的 Exchange Online Management 模块:

Install-Module -Name ExchangeOnlineManagement -Force

运行以下 cmdlet,以验证是否已安装 Exchange Online Management 模块:

Get-Module -ListAvailable -Name ExchangeOnlineManagement

如果安装正确,应显示模块详细信息。

Exchange Online PowerShell 模块使用现代身份验证来连接所有与 Exchange 相关的 PowerShell 环境。

更新模块

要更新现有的 Exchange Online Management 模块,请使用以下 cmdlet:

Update-Module ExchangeOnlineManagement

导入模块

使用以下 cmdlet 将模块加载到你的 PowerShell 会话中:

Import-Module ExchangeOnlineManagement

设置执行策略

连接到 Exchange Online PowerShell 时,PowerShell 设定的执行策略会决定脚本在你的系统上如何运行。 RemoteSigned 是推荐的策略,因为它确保:

  • 本地创建的脚本可以在不需要数字签名的情况下运行
  • 从互联网下载的脚本必须由受信任的发布者进行签名

使用以下 cmdlet 将执行策略设置为 RemoteSigned:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

使用基本身份验证连接(已弃用)

截至 2022 年 10 月,Microsoft 已永久禁用 Exchange Online 的基本身份验证。它已被现代身份验证(OAuth 2.0)取代。

使用现代身份验证进行连接

现代身份验证(OAuth 2.0)是连接到 Exchange Online PowerShell 的推荐且安全的方法。不同的连接方式如下:

Method


Interactive Authentication (GUI Prompt)

This is the default connection method for most tenants, such as standard Microsoft 365 commercial tenants. Use this cmdlet to connect using modern authentication: Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com Replace admin@yourdomain.com with your Exchange Online admin account.A sign-in window will pop up; complete the login process, including multi-factor authentication (MFA) if required.

Device Code Authentication (For Non-GUI Environments)

If you’re working in a non-GUI environment, use the -Device parameter: Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com -Device Copy the provided Device Code.Open https://microsoft.com/devicelogin in a browser.Enter the Device Code.Authenticate with your account and complete MFA (if required).

Connect with Certificate-Based Authentication (Non-Interactive)

For automation or unattended scripts: Connect-ExchangeOnline -CertificateThumbprint “<CertificateThumbprint>” -AppId “<AppId>” -Organization “<YourTenant>” Replace: “<CertificateThumbprint>” with your certificate thumbprint”<AppId>” with the Microsoft Entra ID App ID.”<YourTenant>” with your domain (such as yourdomain.onmicrosoft.com).

不同 Exchange Online 环境的连接示例(GCC、GCC High、DoD)

在使用现代身份验证时,不同的 Exchange Online 环境需要特定的连接 URI 和配置。此类环境的示例如下:

Method

Connection Info

Microsoft 365 GCC (Government Community Cloud)

Use Case: US Government customers (GCC) Connect-ExchangeOnline -UserPrincipalName admin@contoso.onmicrosoft.com Replace admin@contoso.onmicrosoft.com with your Exchange Online admin account.A sign-in window will pop up; complete the login process, including multi-factor authentication (MFA), if required.

Microsoft 365 GCC High

Use Case: US Government customers with high-security requirements Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com -ExchangeEnvironmentName O365USGovGCCHigh -ExchangeEnvironmentName – Specifies the GCC High environment explicitly

Microsoft 365 DoD (Department of Defense)

Use Case: Reserved for the US Department of Defense (DoD) tenants Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com -ExchangeEnvironmentName O365USGovDoD -ExchangeEnvironmentName: Explicitly sets the environment to the Office 365 US Government DoD environment

使用多因素身份验证

启用 MFA 的分步连接流程

使用托管标识(Managed Identities)的连接

使用 Managed Identities 连接到 Exchange Online PowerShell,可在 Microsoft Entra 中实现安全的免密码身份验证。

什么是 Managed Identities?

Managed Identities 允许 Microsoft Entra 资源在不将凭据存储在脚本或代码中的情况下,向受支持的服务进行身份验证。Exchange Online 支持此功能,用于借助 Azure Automation 或类似场景进行无人值守脚本编写。

系统分配与用户分配 Managed Identities 的区别

Feature

System-Assigned

User-Assigned

Tied to a Resource

Linked to a single Microsoft Entra resource (such as VM, Logic App). It is deleted when the resource is deleted.

Can be shared across multiple resources

Management Scope

Automatically managed for its resource

Managed independently and reusable

Use Case

Suitable for single-resource scenarios

Ideal for shared or multi-resource use

使用 Managed Identities 连接 Exchange Online PowerShell

先决条件

  • 确保你的 Azure 资源(例如 VM、Function App、App Service)已启用系统分配或用户分配的 managed identity。
  • 为 Managed Identity 分配 Exchange Online 所需的 Microsoft Entra ID 角色。例如:
  • Exchange 管理员
  • 全局读取者 (Global Reader) 或全局管理员 (Global Administrator)(如果需要更广泛的权限)。
  • 安装并导入 Exchange Online PowerShell 模块。

如何连接

使用带有 -ManagedIdentity 参数的 Connect-ExchangeOnline。

  • 系统分配的托管标识示例:

Connect-ExchangeOnline -ManagedIdentity -Organization <YourDomain>.onmicrosoft.com

  • 用户分配的托管标识示例:
      # Specify the Client ID of the User-Assigned Managed Identity

Connect-ExchangeOnline -ManagedIdentity -Organization <YourDomain>.onmicrosoft.com -ManagedIdentityAccountId <UserAssignedManagedIdentityClientIdValue>
      

适用于无人值守的场景,例如自动化任务或 CI/CD 管道:

  • 将脚本存储在安全的位置,例如 Azure Automation、Azure DevOps,或启用了托管标识的虚拟机(VM)。
  • 使用 Managed Identities 进行身份验证,避免需要显式处理凭据。

示例脚本:

      # Connect to Exchange Online

Connect-ExchangeOnline -ManagedIdentity

# Example Exchange Online commands

Get-Mailbox -RecipientTypeDetails UserMailbox | Select-Object DisplayName, PrimarySmtpAddress

# Disconnect the session

Disconnect-ExchangeOnline -Confirm:$false
      

请参阅 Use Azure managed identities to connect to Exchange Online PowerShell article 以获取更多信息。

面向初学者的 Windows PowerShell 脚本教程(PDF)

了解更多

语法和常用连接参数

语法

Connect-ExchangeOnline cmdlet 具有以下语法:

Connect-ExchangeOnline

[[-ConnectionUri] <String>]

[[-AzureADAuthorizationEndpointUri] <String>]

[[-ExchangeEnvironmentName] <ExchangeEnvironment>]

[[-PSSessionOption] <PSSessionOption>]

[[-DelegatedOrganization] <String>]

[[-Prefix] <String>]

[[-CommandName] <String[]>]

[[-FormatTypeName] <String[]>]

[-AccessToken <String>]

[-AppId <String>]

[-BypassMailboxAnchoring]

[-Certificate <X509Certificate2>]

[-CertificateFilePath <String>]

[-CertificatePassword <SecureString>]

[-CertificateThumbprint <String>]

[-Credential <PSCredential>]

[-Device]

[-启用错误报告]

[-内联凭据]

[-加载 Cmdlet 帮助]

[-日志目录路径 <String>]

[-日志级别 <LogLevel>]

[-ManagedIdentity]

[-ManagedIdentityAccountId <String>]

[-Organization <String>]

[-PageSize <UInt32>]

[-ShowBanner]

[-ShowProgress <Boolean>]

[-SigningCertificate <X509Certificate2>]

[-SkipLoadingCmdletHelp]

[-SkipLoadingFormatData]

[-TrackPerformance <Boolean>]

[-使用多线程 <Boolean>]

[-用户主体名称 UserPrincipalName <String>]

[-使用 RPS 会话]

[<通用参数>]

通用参数

一些基本参数如下:

Parameter

Description

-UserPrincipalName

Specifies the account that you want to use to connect to Exchange Online PowerShell. This parameter lets you skip entering a username in the modern authentication credentials prompt.

-Credential

Specifies the username and password that is used to connect to Exchange Online PowerShell

-DelegatedOrganization

Specifies the customer organization you want to manage when acting as a delegated admin

-ConnectionUri

Specifies the Exchange Online connection endpoint (used in specialized environments like GCC or China tenants)

-CertificateThumbprint

Connects using a certificate instead of username/password. A valid value is the thumbprint value of the certificate.

-AppId

Used with -CertificateThumbprint to specify a Microsoft Entra ID application ID

-AccessToken

Specifies an OAuth 2.0 access token for authentication

-Organization

Specifies the organization when you connect using CBA or managed identity

一些可选参数如下:

Parameter

Description

-ShowProgress

Specifies whether to show or hide the progress bar of imported cmdlets when you connect. Valid values are $true and $false.

-SkipLoadingFormatData

Speeds up connections by skipping the loading of formatting and type data files

-InlineCredential

Directly passes credentials in the command line to avoid prompts when connecting to Exchange Online PowerShell

-LogDirectoryPath

Specifies the location of the log file

-LogLevel

Specifies the logging level. Valid values are Default and All.

-ConnectionTimeout

Specifies the timeout value (in seconds) for the connection attempt

-Device

Typically used on computers without web browsers. You don’t need to specify a value with this switch.

-ManagedIdentity

Specifies that you are using managed identity to connect. You do not need to specify a value with this switch.

高安全性环境中的连接专用参数:

适用于 GCC 环境

Parameter

Description

-ConnectionUri

Specifies the endpoint for GCC tenants

-EntraIDAuthorizationEndpointUri

Specifies the authorization endpoint for GCC

-ExchangeEnvironmentName

Specifies the GCC High environment explicitly

适用于 DoD 环境

Parameter

Description

-ConnectionUri

Points to the DoD-specific endpoint

-ExchangeEnvironmentName

Explicitly sets the environment to DoD

自动化连接

用于无人值守脚本的仅应用程序认证

基于证书的身份验证(CBA)或仅应用程序认证,可通过使用 Microsoft Entra 应用和自签名证书来支持无人值守脚本和自动化场景。

仅应用程序认证允许服务、脚本或后台作业在无需用户交互的情况下,安全访问 API 和资源。这非常适合用于自动化场景,例如计划任务、数据同步或后端处理。

配置仅应用程序认证的步骤

  1. Go to your identity provider, such as Microsoft Entra ID, and register the app. Then note down the Client ID, Tenant ID, and generate a Client Secret or upload a certificate.
  2. 分配所需的 API 权限。应授予“应用程序 API 权限”(Application API permissions),而不是“委托 API 权限”(Delegated API permissions)。
  3. 创建并配置一个自签名的 X.509 证书。请求应用程序专用访问令牌(app-only access token)时,会使用该证书对应用程序在 Microsoft Entra ID 进行身份验证。
  4. 将证书注册到你的应用程序中。这将使你能够在身份验证(authentication)时使用私钥(.pfx 文件)或指纹(thumbprint)。
  5. 要为应用程序分配适当的 RBAC 角色,请使用 Microsoft Entra 中任何受支持的内置角色。

现在,您可以使用“Connect Using Modern Authentication(使用现代身份验证连接)”部分中提供的 cmdlet 连接到 Exchange Online PowerShell。

如需更多信息,请参阅 Microsoft 的文章《Exchange Online PowerShell 和 Security & Compliance PowerShell 中用于无人值守脚本的仅应用认证(App-only authentication)》。

用于无人值守认证的示例 PowerShell 脚本

      $TenantId = “your-tenant-id”

$ClientId = “your-client-id”

$ClientSecret = “your-client-secret”

$Resource = “https://graph.microsoft.com/”

# Get token

$Body = @{

grant_type = “client_credentials”

client_id = $ClientId

client_secret = $ClientSecret

scope = “$Resource/.default”

}

$TokenResponse = Invoke-RestMethod -Uri “https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token” -Method Post -Body $Body

$AccessToken = $TokenResponse.access_token

Write-Output “Access Token: $AccessToken”
      

使用证书进行连接

在自动化中使用证书,相比客户端密钥(client secrets)可提供额外的安全层。由于证书无需以明文形式存储敏感密钥,因此非常适用于脚本或后台作业中的“仅应用认证”(app-only authentication)。

使用证书进行自动化的分步指南

  1. 生成自签名证书。你可以使用 PowerShell、OpenSSL 或任何证书管理工具来生成它。

下面是在 PowerShell 中生成它的方法:

      # Generate a self-signed certificate

$cert = New-SelfSignedCertificate -DnsName “yourapp.domain.com” -CertStoreLocation “Cert:\CurrentUser\My” -KeyExportPolicy Exportable

# Export the certificate and private key as a PFX file

$certPath = “C:\path\to\certificate.pfx”

$certPassword = ConvertTo-SecureString -String “yourpassword” -Force -AsPlainText

Export-PfxCertificate -Cert $cert -FilePath $certPath -Password $certPassword
      
  • 在你的身份提供者(Identity Provider)中注册该应用,例如 Microsoft Entra ID。你还必须上传证书的公钥。具体如下:
  • 进入“应用注册”部分。
  • 使用 .crt 文件(公钥)添加密钥凭据。
  • 记录应用程序(App)ID 和租户(Tenant)ID。
  • 应用程序将使用私钥为仅应用程序身份验证签署 JWT。

证书型连接的安全最佳实践

  • 将证书存储在安全的位置(例如 Azure Key Vault)。避免在脚本中对路径或密钥进行硬编码。
  • 使用有效期较短的证书。在证书到期之前进行轮换。
  • 立即吊销已泄露或未使用的证书。
  • 使用最小特权原则限制对应用及其资源的访问,principle of least privilege并定义精确的 API 范围。
  • 为审计目的记录证书的使用情况。
  • 监控应用认证,以检测未授权访问。
  • 使用加密来保护私钥文件,例如 .pfx。
  • 使用强密码保护私钥。
  • 要求对管理应用程序配置启用 MFA。这可能需要对管理员强制启用 MFA。

管理会话并断开连接

妥善管理并终止 Exchange Online PowerShell 会话对于维护安全性、优化资源使用以及防止会话耗尽至关重要。以下是您应当遵循的一些最佳实践。

管理 Exchange Online PowerShell 会话的最佳实践

  • 使用 Exchange Online Management Module (EXO V2) 以实现现代身份验证并提升安全性。
  • 使用仅具备完成任务所需权限的账号登录,例如 Exchange Admin 和 Security Admin 等角色。
  • Exchange Online 有最大会话限制(每位用户最多 3 个并发会话)。请确保在完成任务后关闭会话,以避免触及该限制。
  • 注意 Exchange Online 会话的默认超时(通常为不活跃 15 分钟)。如果空闲超时频繁发生,请优化脚本以提高效率。
  • 如有可能,请在同一会话中运行脚本。避免让会话长时间处于空闲状态。

终止 Exchange Online PowerShell 会话的最佳实践

  • 完成后务必断开会话连接。仅关闭 PowerShell 窗口而不断开连接,可能会留下孤立会话。务必显式断开连接。
  • Remove-PSSession cmdlet 不能彻底清理 Exchange Online 会话。请始终优先使用:

Disconnect-ExchangeOnline -Confirm:$false

  • 如果您怀疑“孤儿会话”正在消耗资源,请使用以下命令将其终止:

Get-PSSession | Remove-PSSession

  • 如果会话超时,请明确重新连接,而不要假设该会话仍然有效。

为用户启用或禁用对 Exchange Online PowerShell 的访问

要启用或禁用用户对 Exchange Online PowerShell 的访问权限,必须具备足够的管理权限,例如 Global Administrator 或 Exchange Administrator 角色。此外,还需要隶属于“Organization Management(组织管理)”或“Recipient Management(收件人管理)”角色组。

禁用 PowerShell 访问只会影响用户连接到 Exchange Online PowerShell 的能力,不会影响他们对 Microsoft 365 Admin Center 等其他服务的访问。

禁用某个用户的访问权限

当你禁用 PowerShell 访问后,用户将无法使用其凭据连接到 Exchange Online PowerShell。下面是用于禁用访问的 cmdlet:

Set-User -Identity <UserPrincipalName> -EXOModuleEnabled $false

为用户启用访问权限

启用 PowerShell 访问后,用户可以连接到 Exchange Online PowerShell。以下是启用访问权限的 cmdlet:

Set-User -Identity <UserPrincipalName> -EXOModuleEnabled $true

检查用户的 PowerShell 访问状态

要验证用户的 PowerShell 访问权限是启用还是禁用,请使用以下命令:

Get-User -Identity “<UserPrincipalName>” | Format-List EXOModuleEnabled

要获取所有没有访问 Exchange Online PowerShell 的用户,请使用以下 cmdlet:

Get-User -ResultSize unlimited -Filter ‘RemotePowerShellEnabled -eq $false’

要获取所有具有访问 Exchange Online PowerShell 权限的用户,请使用以下 cmdlet:

Get-User -ResultSize unlimited -Filter ‘RemotePowerShellEnabled -eq $true’

与 Exchange Online 断开连接

如果你正在使用 Exchange Online PowerShell V2(EXO V2):

如果你使用 Connect-ExchangeOnline cmdlet 进行了连接,可以使用以下命令断开连接:

Disconnect-ExchangeOnline -Confirm:$false

-Confirm 参数是可选项,用于抑制确认提示。默认情况下,某些 cmdlet 在执行某个操作之前可能会要求你确认(例如断开会话)。

$false 表示你不希望出现确认提示。这会使命令在不询问你是否确定要断开连接的情况下自动执行。

如果你正在使用较旧的 Exchange Online PowerShell 模块:

如果你使用的是较旧的远程 PowerShell 会话(例如使用 New-PSSession 进行连接),你可以通过按如下方式移除该会话来断开连接:

Remove-PSSession $Session

在这种情况下,$Session 是你在首次连接时用于存储 PowerShell 会话对象的变量。

常见连接问题与故障排除

在使用 Exchange Online PowerShell 时,可能会遇到连接问题。让我们来看看这些问题及其解决方案。

未安装或已过期的模块

您可能会看到类似以下的错误:

  • 无法识别该术语“Connect-ExchangeOnline”。
  • 未安装模块“ExchangeOnlineManagement”。

解决方案:

应安装并更新 Exchange Online Management Module(EXO V2)。使用此 cmdlet 来验证模块是否已正确安装:

Get-Module -ListAvailable -Name ExchangeOnlineManagement

凭据不正确或身份验证问题

您可能会遇到以下问题:

  • 发生身份验证错误
  • MFA 提示失败

解决方案:

  • 使用包含正确凭据的现代身份验证。
  • 确认你的帐户支持 MFA,并且没有被封锁。
  • 清除任何已缓存的凭据。
  • 如果您的帐户具有条件访问策略,请验证这些策略是否允许访问 Exchange Online。

会话限制已耗尽

打开的会话过多会阻止您连接,并且您可能会收到如下错误:

  • 已超过允许的最大并发会话数。

解决方案:

  • 断开过期会话。
  • 确保在每次会话结束后正确断开连接。
  • 监控活动会话。

代理服务器或防火墙阻止连接

由于代理或防火墙限制,连接会挂起或失败,并出现此错误:

  • 无法连接到远程服务器。

解决方案:

  • 确保已正确配置代理设置:
  • 通过防火墙允许 Exchange Online PowerShell 终结点:
  • *.outlook.office365.com
  • *.office365.com
  • *.microsoftonline.com
  • 使用以下命令测试连接:

Test-NetConnection outlook.office365.com -Port 443

过时的 TLS 版本

您可能会遇到此错误:

  • 基础连接已关闭:发送时发生意外错误。

解决方案:

  • 请确保您的系统支持 TLS 1.2。
  • 检查您的 .NET 版本(建议使用 4.6.2 或更高版本)。

缺少权限

您可能会遇到类似以下的错误:

  • 访问被拒绝。您没有足够的权限。

解决方案:

  • 你的帐户必须分配有 Exchange Administrator 或相应的角色。

PowerShell 版本不正确

如果你使用的是不受支持的 PowerShell 版本,可能会看到兼容性错误。

解决方案:

  • 请确保使用 PowerShell 5.1 或更高版本:
  • 对于 PowerShell Core(7.x),请确保 EXO V2 Module 兼容。

帐户被锁定或帐户已停用

您会遇到类似以下的错误:

  • 您的帐户已被锁定或已停用。

解决方案:

  • 在 Microsoft 365 管理中心验证帐户状态。
  • 如有必要,请解锁该帐户或重置密码。

DNS 解析问题

您将看到以下错误:

  • 无法解析远程名称。

解决方案:

  • 检查 Exchange Online 端点的 DNS 解析。
  • 如果 DNS 解析失败,请使用公共 DNS 服务器(例如 8.8.8.8 或 1.1.1.1)。

与 PowerShell 模块冲突相关的错误

PowerShell 中的模块冲突可能导致各种错误,例如 cmdlet 无法被识别、功能重叠或版本不匹配。

您可能会遇到类似的错误:

  • 无法识别术语“<cmdlet>”。
  • Cmdlet 在多个模块中可用。
  • cmdlet 引用不明确。

解决方案:

  • 使用此 cmdlet 检查当前已加载的所有模块及其版本:

Get-Module -ListAvailable

  • 如果某个 cmdlet 导致冲突,请找出该 cmdlet 属于哪个模块:

Get-Command <Cmdlet-Name>

  • 显式导入所需模块,并强制使用正确的模块版本:

Import-Module -Name ExchangeOnlineManagement -RequiredVersion 2.x.x -Force

  • 如果加载了过时或发生冲突的模块,请使用以下命令将其移除:

Remove-Module -Name <Module-Name>

  • 如果安装了同一模块的多个版本,请使用以下命令卸载旧版本:

Uninstall-Module -Name ExchangeOnlineManagement -RequiredVersion 1.x.x

REST API 连接错误

要在 PowerShell 中处理 REST API 连接错误,您需要诊断并解决与身份验证、网络连接或请求配置错误相关的问题。

Issue

Solution

Authentication Issues Error: Authentication failures or invalid credentials.

Ensure you’re using the correct credentials and modern authentication (OAuth).Verify that your account has sufficient permissions (for example, Exchange Administrator or similar role).Use Secure Application Model or Certificate-based authentication if accessing programmatically.

Endpoint or Module Issues Error: Unable to connect to the required endpoint.

Verify you are using the correct Exchange Online PowerShell V2 module (ExchangeOnlineManagement).Update the module to the latest version:Check the connectivity to the Exchange Online REST endpoint using the following cmdlet. It should resolve and be reachable:
https://outlook.office365.com/powershell-liveid

Network Connectivity Error: Timeouts or connection refused errors.

The required endpoints should not be blocked by firewalls or proxies. These include: *.office365.com *.microsoftonline.com Test internet connectivityIf behind a corporate proxy, ensure the proxy is configured correctly for PowerShell.

Token Expiry Error: Authentication token expiration during the session.

Use Connect-ExchangeOnline with a persistent session, as shown below: Connect-ExchangeOnline -UserPrincipalName <your-admin-email> -ShowProgress $true Refresh your session if the token expires using the following cmdlet: Disconnect-ExchangeOnline Connect-ExchangeOnline

TLS/SSL Protocol Issues Error: TLS errors during connection.

Ensure TLS 1.2 is enabled

Service Outages Error: Service unavailable or intermittent connectivity.

Check the Microsoft 365 Service Health Dashboard for any ongoing issues.

HTTP Errors These errors occur when the server returns an HTTP status code indicating failure. Common Codes: 400 Bad Request: The request is malformed (e.g., invalid JSON or parameters)401 Unauthorized: Invalid or missing authentication credentials403 Forbidden: Access is denied even though authentication is correct404 Not Found: The requested endpoint or resource does not exist500 Internal Server Error: An issue on the API server

Ensure the payload and headers meet the API documentation requirements.Verify API keys, tokens, or other credentials.Ensure your account has access rights to the requested resource.Use logs or a tool like Postman to check raw request/response data.

SSL/TLS Errors Secure connections (HTTPS) may fail due to certificate issues.

Ensure the server’s SSL certificate is valid and trusted.For local testing, you can bypass SSL validation, but avoid this in production.Ensure your client libraries (e.g., Python requests, Node.js https) are up to date to support modern TLS versions.

Timeout Errors These occur when the API server does not respond within the expected timeframe.

Adjust the timeout parameter in your API client to increase timeout settings.Avoid sending excessively large payloads or slow queries.Contact the API provider if timeouts are frequent as there may be server load issues.

Rate-Limiting and Quotas APIs often have usage limits, which can lead to errors when exceeded. Error: HTTP 429 Too Many Requests

Check Rate Limits. Refer to the API documentation for request limits.Implement Throttling to space out requests to comply with the rate limits.Contact the API provider to request higher quotas.

Exchange Online 的关键 PowerShell Cmdlet

要获取所有可用的 Exchange Online PowerShell cmdlet 列表,请使用以下 cmdlet:

Get-command -Module ExchangeOnlineManagement

下面是对 Exchange Online 关键 cmdlet 的快速概览,其中包括 Get-MailboxGet-EXOMailboxStatistics 以及其他命令,用于有效管理邮箱、用户及其配置。

Get-Mailbox

此 cmdlet 用于检索 Exchange Online 中邮箱的信息。你还可以显示特定邮箱,例如用户邮箱、共享邮箱等。

语法

Get-Mailbox [-Identity] <String> [-RecipientTypeDetails <RecipientTypeDetails>] [other parameters]

常见用法示例

  • 获取所有邮箱:

Get-Mailbox -ResultSize Unlimited

  • 获取所有共享邮箱:

Get-Mailbox -RecipientTypeDetails SharedMailbox

  • 按域筛选邮箱:

Get-Mailbox -Filter “EmailAddress -like ‘*@domain.com'”

Get-EXOMailboxStatistics

该 cmdlet 会检索 Exchange Online 中邮箱的详细统计信息,提供诸如邮箱大小、项目数以及最后登录时间等数据。

语法

Get-EXOMailboxStatistics [-Identity] <String>

常见用法示例

  • 获取某个用户的邮箱统计信息:

Get-EXOMailboxStatistics -Identity user@domain.com

  • 检索所有邮箱的大小:

Get-EXOMailboxStatistics | Select DisplayName, ItemCount, TotalItemSize

  • 获取大小超过特定阈值的邮箱:

Get-EXOMailboxStatistics | Where-Object { $_.TotalItemSize -gt 10GB }

Get-MailboxStatistics

此 cmdlet 类似于 Get-EXOMailboxStatistics,但可用于本地 Exchange 或混合环境。它提供邮箱大小和项目数量的详细信息。

语法

Get-MailboxStatistics [-Identity] <String>

常见用法示例

  • 获取特定用户的统计信息:

Get-MailboxStatistics -Identity “user@domain.com”

  • 获取所有邮箱大小和最后登录时间:

Get-MailboxStatistics | Select DisplayName, LastLogonTime, TotalItemSize

Get-MailboxPermission

该 cmdlet 用于检索分配给邮箱的权限,包括委派访问权限。

语法

Get-MailboxPermission [-Identity] <String>

常见用法示例

  • 查看邮箱的所有权限:

Get-MailboxPermission -Identity user@domain.com

  • 筛选非默认权限:

Get-MailboxPermission -Identity “user@domain.com” | Where-Object { $_.IsInherited -eq $false }

Set-Mailbox

该 cmdlet 会修改邮箱设置,例如配额限制、转发以及启用功能。

语法

Set-Mailbox [-Identity] <String> [-Parameters]

常见用法示例

  • 启用邮箱转发:

Set-Mailbox -Identity “user@domain.com” -ForwardingSMTPAddress “forwardto@domain.com” -DeliverToMailboxAndForward $true

  • 更改邮箱配额:

Set-Mailbox -Identity “user@domain.com” -ProhibitSendQuota 50GB

New-Mailbox

此 cmdlet 会在 Exchange Online 中为用户创建新的邮箱。该 cmdlet 可用于多种场景,包括为单个用户创建邮箱、创建共享邮箱,以及创建诸如房间或设备邮箱之类的资源邮箱。

语法

New-Mailbox -Name <Name> -MicrosoftOnlineServicesID <UserPrincipalName> -Password (ConvertTo-SecureString -String “<Password>” -AsPlainText -Force)

常见用法示例

  • 创建用户邮箱:

New-Mailbox -Name “John Doe” -MicrosoftOnlineServicesID “johndoe@domain.com” -Password (ConvertTo-SecureString -String “P@ssw0rd!” -AsPlainText -Force)

  • 创建共享邮箱:

New-Mailbox -Shared -Name “Support Team” -MicrosoftOnlineServicesID “support@domain.com”

  • 创建房间邮箱:

New-Mailbox -Room -Name “Conference Room 1” -MicrosoftOnlineServicesID conference1@domain.com

  • 为团队创建共享邮箱:

New-Mailbox -Shared -Name “HR Team” -MicrosoftOnlineServicesID “hr@domain.com” -Alias “HRTeam”

Remove-Mailbox

此 cmdlet 会从 Exchange Online 删除邮箱。此操作可应用于多种邮箱类型,包括用户邮箱、共享邮箱以及资源邮箱(例如会议室或设备邮箱)。

语法

Remove-Mailbox -Identity <MailboxIdentity>

常见用法示例

  • 删除用户邮箱(软删除邮箱,使其在保留期内可恢复):

Remove-Mailbox -Identity “johndoe@domain.com”

  • 永久删除邮箱(删除时不提供保留期):

Remove-Mailbox -Identity “johndoe@domain.com” -Permanent

  • 仅删除归档邮箱(保留指定用户的主邮箱不变):

Remove-Mailbox -Identity “johndoe@domain.com” -Archive

Get-MailTrafficSummaryReport

此 cmdlet 会检索贵组织的邮件流量摘要(在 Microsoft 365 环境中可用)。

语法

Get-MailTrafficSummaryReport [-StartDate] <DateTime> [-EndDate] <DateTime>

常见用法示例

  • 获取过去 7 天的邮件流量:
      $StartDate = (Get-Date).AddDays(-7)

$EndDate = Get-Date

Get-MailTrafficSummaryReport -StartDate $StartDate -EndDate $EndDate
      

Search-Mailbox

该 cmdlet 会在单个邮箱或多个邮箱中搜索特定内容。

语法

Search-Mailbox [-Identity] <String> [-SearchQuery <Query>] [-TargetMailbox <String>]

常见用法示例

  • 搜索包含特定关键词的电子邮件:

Search-Mailbox -Identity “user@domain.com” -SearchQuery “Subject:’Invoice'”

  • 将搜索结果复制到另一个邮箱:

Search-Mailbox -Identity “user@domain.com” -SearchQuery “Keyword” -TargetMailbox “admin@domain.com” -TargetFolder “SearchResults”

Get-MailboxAutoReplyConfiguration

此 cmdlet 用于检索邮箱的自动回复(外出办公)设置。

语法

Get-MailboxAutoReplyConfiguration [-Identity] <String>

常见用法示例

  • 获取特定用户的自动回复设置:

Get-MailboxAutoReplyConfiguration -Identity “user@domain.com”

Get-Recipient

此 cmdlet 会检索所有收件人(邮箱、组、联系人等)。

语法

Get-Recipient [-Filter] <String>

常见用法示例

  • 获取所有收件人:

Get-Recipient

  • 筛选特定类型的收件人:

Get-Recipient -RecipientTypeDetails MailUser

Cmdlet 过滤器

使用过滤器根据特定属性缩小结果范围。

语法

过滤器用 { } 括起来,并使用“属性 运算符 值”的结构:

-Filter {Property -Operator ‘Value’}

示例

Get-Mailbox -Filter {DisplayName -like “*Test*”}

常用运算符

  • -eq: 等于
  • -ne: 不等于
  • -like: 通配符匹配(* 表示零个或多个字符)
  • -notlike: 否定 -like
  • -gt: 大于
  • -lt: 小于

使用 Cmdlets 的筛选器示例

  • 检索特定域的邮箱

Get-Mailbox -Filter {EmailAddresses -like ‘*@example.com’}

  • 查找在特定日期之后创建的邮箱

Get-Mailbox -Filter {WhenCreated -gt ‘2023-01-01’}

  • 获取具有特定显示名称模式的邮箱

Get-Mailbox -Filter {DisplayName -like ‘*Test*’}

  • 获取已禁用的邮箱

Get-Mailbox -Filter {AccountDisabled -eq $true}

合并多个条件的示例

你可以使用诸如 -and 和 -or 之类的逻辑运算符来组合筛选条件。

  • 获取具有特定 UPN 和显示名称的用户

Get-Mailbox -Filter {UserPrincipalName -like ‘*@example.com’ -and DisplayName -like ‘*John*’}

  • 查找已启用但超出大小阈值的邮箱

Get-Mailbox -Filter {AccountDisabled -eq $false -and ProhibitSendQuota -gt 10GB}

使用管道进行筛选的示例

筛选器也可以与 Where-Object 结合,以实现更高级的筛选。

  • 按自定义属性筛选邮箱

Get-Mailbox | Where-Object { $_.CustomAttribute1 -eq ‘Value1’ }

  • 获取最近 30 天内登录过的活动邮箱

Get-MailboxStatistics | Where-Object { $_.LastLogonTime -gt (Get-Date).AddDays(-30) }

高级配置

  • 使用 Select-Object 精炼输出,仅显示相关属性。

Get-Mailbox | Select-Object DisplayName, PrimarySmtpAddress

  • 将结果导出为 CSV 以便生成报告。

Get-Mailbox | Export-Csv -Path “Mailboxes.csv” -NoTypeInformation

  • 默认限制为 1000 条结果;使用 -ResultSize Unlimited 获取所有项目。

使用 PowerShell 迁移到 Exchange Online

使用 PowerShell 迁移到 Exchange Online 需要执行多个步骤,包括准备本地环境、配置迁移端点以及管理迁移过程。

先决条件

在执行迁移之前,请确保你已具备:

  • 包含 Exchange Online 的有效 Microsoft 365 或 Office 365 订阅。
  • 同时适用于本地 Exchange 服务器和 Exchange Online 的管理员凭据。
  • 已安装 Exchange Online PowerShell V2 模块(EXO V2)。
  • 如果使用混合迁移,需要先设置 Exchange Online(混合)配置。

混合迁移(适用于同时拥有本地 Exchange 和 Exchange Online 的组织)

在混合环境中,你可以在保持本地与云端邮箱并存的同时迁移邮箱。

  1. 安装并配置 Hybrid Configuration Wizard (HCW)。

Hybrid Configuration Wizard (HCW) 是配置混合 Exchange 环境的主要工具。它可确保你的本地 Exchange 服务器已准备好与 Exchange Online 进行混合共存。你可以从 Exchange Admin Center (EAC) 下载并运行它。

  • 准备本地 Exchange 服务器:
  • 运行 Get-ExchangeServer cmdlet,以确保使用的是正确版本。
  • 运行 New-RemoteMailbox cmdlet,为正在迁移的用户创建远程邮箱。
  • 通过使用 New-MigrationBatch cmdlet 创建迁移批次来启动迁移:

New-MigrationBatch -Name “MigrationBatch” -SourceEndpoint <OnPremisesExchangeEndpoint> -TargetEndpoint <ExchangeOnlineEndpoint> -MailboxList <MailboxesToMigrate> -AutoStart -AutoComplete

替换占位符:

  • <OnPremisesExchangeEndpoint> — 你本地(on-premises)Exchange 服务器的端点
  • <ExchangeOnlineEndpoint> – Exchange Online 的端点
  • <MailboxesToMigrate> – 需要迁移的邮箱列表
  • 用于监控迁移过程:

Get-MigrationBatch | Get-MigrationUser

  • 迁移完成后,可以使用以下命令将其完成:

Set-MigrationBatch -Identity “MigrationBatch” -Complete

一次性切换迁移(适用于小型环境,通常少于 150 个邮箱)

在一次性切换迁移中,所有邮箱都会在一个批次中从本地(on-premises)Exchange 迁移到 Exchange Online。

  1. 使用 New-MigrationEndpoint cmdlet 为本地(on-premises)Exchange 服务器创建一个端点:

New-MigrationEndpoint -Name “CutoverEndpoint” -ExchangeServer “<OnPremisesExchangeServer>” -Type “ExchangeRemoteMove”

  • 使用 New-MigrationBatch cmdlet 为所有邮箱创建一个迁移批次:

New-MigrationBatch -Name “CutoverMigrationBatch” -SourceEndpoint “CutoverEndpoint” -MailboxList “user1@example.com”, “user2@example.com” -TargetDeliveryDomain “<ExchangeOnlineDomain>” -AutoStart -AutoComplete

  • 使用 Get-MigrationBatch cmdlet 监控迁移:

Get-MigrationBatch “CutoverMigrationBatch” | Get-MigrationUser

  • 迁移完成后,可以使用 Set-MigrationBatch cmdlet 完成迁移:

Set-MigrationBatch -Identity “CutoverMigrationBatch” -Complete

分阶段迁移(适用于中型环境)

在分阶段迁移中,邮箱会从本地(on-premises)Exchange 分阶段(通常以批次为单位)迁移到 Exchange Online。

  1. 为本地(on-premises)Exchange 创建一个端点:

New-MigrationEndpoint -Name “StagedEndpoint” -ExchangeServer “<OnPremisesExchangeServer>” -Type “ExchangeRemoteMove”

  • 要分阶段迁移邮箱,请使用 New-MigrationBatch cmdlet 创建迁移批次:

New-MigrationBatch -Name “StagedMigrationBatch” -SourceEndpoint “StagedEndpoint” -MailboxList “user1@example.com”, “user2@example.com” -TargetDeliveryDomain “<ExchangeOnlineDomain>” -AutoStart -AutoComplete

  • 使用以下方式监控迁移:

Get-MigrationBatch “StagedMigrationBatch” | Get-MigrationUser

  • 迁移完成后,进行最终确认:

Set-MigrationBatch -Identity “StagedMigrationBatch” -Complete

安全最佳实践

确保安全连接

使用安全的身份验证方法

使用 OAuth 的现代身份验证,而不是基本身份验证。此外,必须为所有访问 Exchange Online 的用户帐户启用多因素身份验证(MFA)。

使用条件访问策略

在 Microsoft Entra 中配置条件访问,以强制执行诸如位置、设备合规性和用户风险级别等限制。请确保阻止或限制来自未知或存在风险的位置的访问。

限制 PowerShell 访问

使用基于角色的访问控制(RBAC),将 PowerShell 访问权限限制为仅对需要使用的用户开放。作为进一步的预防措施,请对不需要管理访问权限的帐户禁用远程 PowerShell。

强制启用 TLS 加密

确保所有到 Exchange Online 的连接都使用 TLS 1.2 或更高版本。同时,你还应定期审计系统,以确认符合现代加密协议。

使用 Privileged Access Workstations (PAWs)

将管理任务限制在安全且隔离的工作站上,以减少遭受恶意软件或攻击的暴露风险。

使用安全的应用程序令牌

对于无人值守脚本,请将用户凭据替换为 Microsoft Entra ID 中的安全应用程序注册。

在 Exchange Online 中管理用户权限和访问控制

实施基于角色的访问控制(RBAC)

根据最小特权原则为用户分配预定义角色。只有在绝对必要时,才为其分配诸如 Global Administrator 这样的宽泛权限角色。

定期监控并审查权限

定期审计权限并移除不必要的访问。作为常规做法,使用 Microsoft 365 Security & Compliance Center 的报告来查看访问日志。

分离管理职责

为管理任务和日常用户活动使用不同的账户。更进一步,为诸如邮箱管理和合规管理等不同管理功能分配不同角色。

使用即时(Just-in-Time,JIT)访问

使用 Microsoft Entra ID Privileged Identity Management (PIM) 实施 JIT 访问 策略,以提供临时的高权限。

启用邮箱审计

为所有邮箱启用审计,以跟踪更改并检测未授权访问。

在生产环境中安全使用 PowerShell 的建议

保护好你的脚本

避免在代码中硬编码凭据;请使用 Azure Key Vault 或 Windows Credential Manager 等安全的存储机制。同时使用参数化脚本并进行安全的输入处理,以避免注入漏洞。

监控并记录 PowerShell 活动

启用 PowerShell 日志记录(模块、脚本块和转录(Transcript)日志)。您还可以将日志与 SIEM 系统集成,以实现实时监控。

使用已签名的脚本

使用受信任的证书对 PowerShell 脚本进行签名,以确保其完整性。为支持这一点,请将 PowerShell 执行策略设置为 AllSigned,因为它只允许已签名的脚本。

以最小特权运行 PowerShell

避免在不必要的情况下使用具有更高权限的账户。相反,应为特定任务使用细粒度权限。

保持 PowerShell 和模块为最新

请定期将 PowerShell 更新到最新版本,以解决安全漏洞。除此之外,还要更新 Exchange Online Management 模块,以利用最新功能和修复。

限制网络访问

使用防火墙规则或 Microsoft Entra ID Named Locations,将对 Exchange Online 端点的访问限制为已知 IP 地址。

加密敏感数据

使用 SecureString 或其他加密方法,以安全的方式存储和传输敏感数据。

Netwrix Auditor for Exchange

结论

Exchange Online 的 PowerShell 提供强大的功能,用于管理并自动化管理任务,使管理员能够处理诸如批量用户更新、报表生成和配置更改等复杂操作。通过采用包括 OAuth 和 MFA 在内的现代身份验证方式,组织可以确保对 Exchange Online 的访问安全。利用 PowerShell 自动化可以最大限度地减少手动工作,降低人为错误,并提升整体运维一致性与可扩展性。

如需了解更高级的配置并深入学习,请访问 Microsoft 的官方文档、PowerShell 培训模块以及社区论坛。

Microsoft 文档

常见问题

如何一步一步连接到 Exchange Online PowerShell?

按照正确的顺序连接到 Exchange Online PowerShell 非常简单。首先,确保通过运行以下命令安装 Exchange Online PowerShell 模块:Install-Module -Name ExchangeOnlineManagement 在以管理员权限运行的 PowerShell 会话中执行。安装完成后,使用 Connect-ExchangeOnline -UserPrincipalName your-admin@domain.com 来建立连接。若您的账户启用了多重身份验证,将会提示您进行验证。认证成功后,使用以下命令验证连接:Get-Mailbox -ResultSize 1 以测试基础功能。完成操作后,请务必使用 Disconnect-ExchangeOnline 断开连接,以保持安全最佳实践。

Exchange Online PowerShell 访问被拒绝——如何修复?

访问被拒绝错误通常源于权限不足或认证问题。首先确认您的账户具有所需的 Exchange Online 管理员角色——至少需要 Exchange Administrator 或 Global Administrator 权限。如果角色正确,请检查贵组织是否使用了可能会阻止 PowerShell 连接的条件访问(Conditional Access)策略。使用以下命令清除任何已缓存的凭据:Remove-StoredCredential 然后再尝试重新连接。若问题持续存在,请确保您使用的是 ExchangeOnlineManagement 模块的最新版本,因为较旧的版本可能与现代身份验证要求存在兼容性问题。

无法连接到 Exchange Online PowerShell——常见解决方案有哪些?

连接失败通常归为三类:身份验证问题、网络问题或模块冲突。首先确认您使用的是正确的语法:Connect-ExchangeOnline -UserPrincipalName 而不是已弃用的连接方法。检查网络连通性和防火墙设置——Exchange Online 需要访问特定的 Microsoft 端点。如果您位于企业代理之后,请配置 PowerShell 使用您的代理设置。当同时安装了旧的 MSOnline 模块和新的 ExchangeOnlineManagement 模块时,可能会发生模块冲突。卸载旧版模块,并坚持使用统一的 ExchangeOnlineManagement 模块,以获得最顺畅的体验。

如何安装 Exchange Online PowerShell 模块?

安装 Exchange Online PowerShell 模块只需要一条命令,但关键在于正确处理前置条件。请使用管理员身份运行 PowerShell,然后执行 Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber 。-Force 参数可确保获取最新版本,而 -AllowClobber 用于处理与现有 cmdlet 的任何冲突。如果遇到执行策略错误,请使用 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 暂时设置策略。在无法修改执行策略的环境中,请从 PowerShell Gallery 手动下载模块,并使用离线方式进行安装。安装完成后请始终通过运行 Get-Module ExchangeOnlineManagement -ListAvailable 来确认模块已正确安装。

如何使用 MFA 连接到 Exchange Online PowerShell?

在正确配置的情况下,使用 Exchange Online PowerShell 进行多因素身份验证(MFA)非常简单。现代的 Connect-ExchangeOnline cmdlet 会自动处理 MFA——只需使用 Connect-ExchangeOnline -UserPrincipalName your-admin@domain.com,然后你会被重定向到浏览器窗口以完成 MFA。对于无需监控的脚本或自动化,请配置基于证书的身份验证,或使用带有 Connect-ExchangeOnline -CertificateThumbprint or Connect-ExchangeOnline -AppId 的服务主体。请避免使用基本身份验证或应用密码,因为 Microsoft 正在逐步淘汰这些传统方法。现代身份验证不仅更安全——而且更可靠,并能与组织的身份管理策略实现更好的集成。

分享到

了解更多

关于作者

Asset Not Found

Jonathan Blackwell

软件开发负责人

自 2012 年以来,工程师兼创新者 Jonathan Blackwell 一直提供工程领导力,使 Netwrix GroupID 成为 Active Directory 和 Azure AD 环境中群组与用户管理领域的领先者。他在研发、市场与销售方面的经验,使 Jonathan 能够全面理解 Identity 市场以及买家如何思考。