如何关闭 Office365 Exchange Powershell 连接

How can I close Office365 Exchange Powershell connections

当我连接到 Office365 Exchange 时出现一条错误消息。我怎样才能关闭这个连接?

脚本:

$userCredential = Get-AutomationPSCredential -Name 'automation'


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

Get-Mailbox -ResultSize Unlimited -Filter {RecipientTypeDetails -eq "UserMailbox" -or RecipientTypeDetails -eq "SharedMailbox" -or RecipientTypeDetails -eq "RoomMailbox" -or RecipientTypeDetails -eq "DiscoveryMailbox"} | Set-Mailbox -AuditEnabled $true -AuditLogAgeLimit 180 -AuditAdmin Update, MoveToDeletedItems, SoftDelete, HardDelete, SendAs, SendOnBehalf, Create, UpdateFolderPermission -AuditDelegate Update, SoftDelete, HardDelete, SendAs, Create, UpdateFolderPermissions, MoveToDeletedItems, SendOnBehalf -AuditOwner UpdateFolderPermission, MailboxLogin, Create, SoftDelete, HardDelete, Update, MoveToDeletedItems

错误:

New-PSSession : [outlook.office365.com] Processing data from remote server outlook.office365.com failed with the 
 following error message: 
 [AuthZRequestId=fe5eb99c-df73-4899-b445-ad91e0ecf1f3][FailureCategory=AuthZ-AuthorizationException] Fail to create a 
 runspace because you have exceeded the maximum number of connections allowed : 3 for the policy party : MaxConcurrency. 
 Please close existing runspace and try again.
 Policy: CN=GlobalThrottlingPolicy_cfaee113-6e8e-4889-aa2c-99baf345f85f,CN=Global Settings,CN=ExchangeLabs,CN=Microsoft 
 Exchange,CN=Services,CN=Configuration,DC=eurprd04,DC=prod,DC=outlook,DC=com; 
 LockRemaining: 00:00:00 For more information, see the about_Remote_Troubleshooting Help topic.
 At line:4 char:15
 + ... xoSession = New-PSSession -ConfigurationName Microsoft.Exchange -Conn ...
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], 
 PSRemotingTransportException
 + FullyQualifiedErrorId : IncorrectProtocolVersion,PSSessionOpenFailed

您可以 close/remove 在 完成您的代码后 会话 :

# your code
# ...

if ($ExoSession) { Remove-PSSession -Session $ExoSession -ErrorAction SilentlyContinue } }

或者,通过在之前添加此行 创建新会话以首先删除所有可能的现有会话:

Get-PSSession | Where-Object { $_.ConfigurationName -eq 'Microsoft.Exchange' } | Remove-PSSession -ErrorAction SilentlyContinue

希望对您有所帮助