网站搜索

使用远程 PowerShell 连接到 Exchange 服务器和 Microsoft 365


在本文中,我们将展示如何从 PowerShell 控制台远程连接到本地 Exchange Server 或 Microsoft 365 (Exchange Online)。

如何通过远程PowerShell(无需EMS)连接到Exchange服务器?

您可以使用 Exchange 命令行管理程序 (EMS) cmdlet 管理本地 Exchange 组织(Exchange Server 2010、2013、2016 或 2019)。 EMS 作为 Exchange 管理工具的一部分安装。如果您的计算机上未安装 Exchange 命令行管理程序,您可以远程连接到 Exchange 服务器并将 cmdlet 从 Exchange 主机导入到本地 PowerShell 会话。

Exchange Server 中的远程连接是通过名为 PowerShell 的单独虚拟 IIS(Internet 信息服务)目录建立的。默认情况下,使用Kerberos身份验证,并使用WinRM进行通信。

提示。请注意,远程 PowerShell 会话不完全支持某些 EMS cmdlet。例如,获取 ExchangeCertificate。要使用它,您必须在计算机上安装 Exchange Management Shell。

在开始之前,请确保您的本地 PowerShell 执行策略允许您运行本地 PS 脚本。下面的命令允许为当前用户运行本地脚本。

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

打开计算机上的 PowerShell 控制台并运行以下命令:

$UserCredential = Get-Credential

输入您要用于连接 Exchange 的帐户的登录名和密码。

创建与 Exchange 服务器的远程 PowerShell 会话:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mun-mbex1.a-d.site/PowerShell/ -Authentication Kerberos -Credential $UserCredential

注意

http

代替

https

用于访问 Web PowerShell。

确保会话已创建并且现在处于已打开状态:

Get-PSSession

将远程 PowerShell 会话导入本地会话:

Import-PSSession $Session

然后,您可以在本地 PowerShell 会话中使用所有 Exchange 管理 cmdlet。

请记住正确结束远程 PowerShell 会话。如果您只是关闭 Windows PowerShell 控制台而不断开会话连接,则可能会超出远程 PowerShell 会话的限制。

要结束会话,请运行以下命令:

Remove-PSSession $Session

确保没有剩余正在运行的远程 PowerShell 会话:

Get-PSSession

您可以使用 PowerShell 配置文件自动将 PowerShell cmdlet 从远程 Exchange 导入到您的会话中。

创建一个配置文件:

New-Item -Path $profile -ItemType file -force

使用记事本打开 Microsoft.PowerShell_Profile.ps1:

notepad $profile

将命令添加到文件以连接到 Exchange 并将 cmdlet 从远程会话导入到本地 PowerShell 会话:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mun-mbex1.a-d.site/PowerShell/ -Authentication Kerberos -Credential (Get-Credential)
Import-PSSession $Session

使用远程 PowerShell 连接到 Exchange Online (Microsoft 365)

以同样的方式,您可以连接到 Exchange Online (Microsoft 365) 租户来管理邮箱、会议室、通讯组列表和其他 Microsoft 365 设置。

让我们了解如何使用基本身份验证从 PowerShell 控制台远程连接到 Exchange Online,而无需安装 Microsoft Exchange Online PowerShell 模块 (EXO/ EXOv2)。

允许本地 PS 脚本运行:

Set-ExecutionPolicy RemoteSigned

获取您的 Exchange Online 管理员凭据:

$UserCredential = Get-Credential

必须首先允许此 Azure 用户通过 PowerShell 进行远程连接:

Set-User -Identity maxbak@a-d.site -RemotePowerShellEnabled $true

然后,您可以与 Microsoft 365 建立远程 PowerShell 会话:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

如果您的帐户启用了多重身份验证(Microsoft 建议对所有管理员帐户使用 MFA),则尝试使用以下方式进行连接时会出现以下错误

New-PSSession
New-PSSession : [outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is denied.
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed

然后您必须安装 Exchange Online PowerShell V2 (EXO V2) 模块:

Install-Module ExchangeOnlineManagement

在这种情况下,使用以下 cmdlet 连接到 Exchange Online:

Connect-ExchangeOnline -UserPrincipalName maxbak@a-d.site -ShowProgress $true

或者您可以禁用该帐户的 MFA:

Set-MsolUser -UserPrincipalName maxbak@a-d.site -StrongAuthenticationRequirements @()

然后将远程会话导入到您的控制台:

Import-PSSession $Session

与 Exchange Online 的远程 PowerShell 连接的最大数量限制为三个会话。如果超出限制,则会出现以下错误:

Fail to create a runspace because you have exceeded the maximum number of connections allowed.

您现在可以管理您的 Microsoft 365 邮箱。

要结束所有远程 PowerShell 会话,请运行以下命令:

Get-PSSession | Remove-PSSession

相关文章