You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell V4执行Connect-ExchangeOnline报New-ExoPSSession索引超出范围错误

问题根因

你遇到的报错核心原因是 ExchangeOnlineManagement 2.0.3版本的最低运行环境要求为PowerShell 5.1,不兼容PowerShell 4.0版本:

  • 2.x版本的New-ExoPSSession内部创建远程会话的逻辑,依赖PowerShell 5.1才有的运行时特性、.NET 类库实现,在PowerShell 4.0环境下执行时,内部处理返回数据的集合结构和预期不一致,就会抛出你遇到的索引越界错误:

New-ExoPSSession: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

  • 你测试过的1.0.1版本虽然兼容PowerShell 4.0,但确实不支持证书+应用ID的无交互连接参数,无法满足你的使用需求。
可行解决方案

方案1:升级PowerShell到5.1版本(最优)

你已经验证同版本脚本在PowerShell 5.1环境下运行正常,升级后无需调整现有脚本逻辑即可直接使用。PowerShell 5.1可以和你当前安装的.NET Framework 4.7.1完全兼容,升级不会影响现有其他基于.NET 4.7.1运行的程序。

方案2:暂无法升级PowerShell的替代方案

如果当前环境暂不允许升级PowerShell,可以改用不依赖ExchangeOnlineManagement模块的旧版远程PowerShell连接方式,直接通过New-PSSession指定连接端点、证书参数完成认证,示例代码如下:

$connectionUri = "https://outlook.office365.com/powershell-liveid?DelegatedOrg=$myTenant"
$session = New-PSSession -ConnectionUri $connectionUri -CertificateThumbprint $myCert.Thumbprint -AppId $AppId -Organization $myTenant -AllowRedirection
Import-PSSession $session -DisableNameChecking

使用该方式需要提前确认你在Azure AD中注册的应用已经正确配置Exchange Online的对应应用权限,且允许使用证书进行远程PowerShell认证。

内容的提问来源于stack exchange,提问作者VGimenez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 15:15:00