在 PowerShell 中使用 Connect-ExchangeOnline 命令时出现 New-ExoPSSession 错误

New-ExoPSSession error when using Connect-ExchangeOnline command in PowerShell

我正在尝试使用 Connect-ExchangeOnline 命令连接到 PowerShell,但收到以下错误。有什么想法吗?

New-ExoPSSession : . 
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement.0.1\ExchangeOnlineManagement.psm1:445 char:30
+ ... PSSession = New-ExoPSSession -ExchangeEnvironmentName $ExchangeEnviro ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-ExoPSSession], Exception
    + FullyQualifiedErrorId : System.Exception,Microsoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSession

请尝试使用如下cmdlet:

Connect-IPPSSession -PSSessionOption
$EXOSession = New-ExoPSSession -pssessionoption
Import-PSSession $EXOSession -Prefix EXO

这是我用来连接的脚本。请填写您的默认用户名,并确保 运行 顶部评论中的先决条件(当然要先查看)

如果您使用的是非交互式,您将需要了解如何 store your credentials securely and how to use 那些存储的凭据,将 Get-Credential 部分替换为您的配置

# Requires: .Net 4.5, Windows Management Framework 5.1 (see https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps )
#
# Run Once:
#  Set-ExecutionPolicy RemoteSigned; Get-ExecutionPolicy
#  Install-Module PowerShellGet -Force
#  Install-Module –Name ExchangeOnlineManagement
#
# https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/exchange-online-powershell-v2/exchange-online-powershell-v2?view=exchange-ps



$exo = New-Module -AsCustomObject -ScriptBlock {
    $UserName = "Default.Username@domain"
    $UserCredential = Get-Credential -message "Enter 365 admin credentials" -UserName $UserName
    function IsConnected(){
        try{
            if (
                @($(get-mailbox -resultsize 1 -WarningAction silentlycontinue)).count `
                -eq
                1
            ) {return $true}
        }
        catch {}
        return $false
    }

    function Connect(){
        $result = "Unfinished"
        if ($this.IsConnected()) {
            $result = "Success"
        } else {
            $UserCredential = $this.UserCredential
            Connect-ExchangeOnline -Credential $UserCredential
            if ($this.IsConnected()) {
                $result = "Success"
            } else {
                $result = "Fail"
            }
        }
        switch($result){
            "Unfinished" {Write-Warning "`nAn unknown error occured in .Connect(), Appears to have ended while unfinished";break}
            "Success" {Write-host "`nSuccessfully connected to Exchange 365";break}
            "Fail" {Write-Warning "`nFailed to connect to Exchange 365";break}
            default {write-warning "`nAn unknown error occured in .Connect(), result code unrecognized";break}
        }
        # old style
        #$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
        #Import-PSSession $Session -DisableNameChecking -AllowClobber
    }

    function ConnectMsol(){
        Connect-MsolService -Credential $UserCredential
    }

    function Disconnect(){
        Disconnect-ExchangeOnline
        # old sytle
        #Remove-PSSession $Session
    }

    function Cycle(){
        $this.Disconnect()
        $this.Connect()
    }
    Export-ModuleMember -Function * -Variable *
}

$exo.Connect()
#$exo.ConnectMsol()

<#
# --- Azure AD ---
# https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell#connect-with-the-azure-active-directory-powershell-for-graph-module
# --- Azure AD ---
#>

从 Intune 中删除安全基线并通过重新启动重新同步解决了我的问题。

最新版本的 Exchange Online 模块需要 PowerShell V7。一旦我安装了 V7,它就可以正常工作。

每个:https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module

EXO V2 module is supported in PowerShell 7.0.3 or later

通过将 Windows 10 中的默认 Web 浏览器切换到 Chromium Edge 浏览器,我能够解决类似的问题。我调出登录 window 一次,然后将 Firefox 切换回我的默认浏览器,它仍然有效。

使用 ExchangeOnlineManagement 模块不需要 PowerShell v7,但这是一个可接受的解决方法。它使用登录到默认 Web 浏览器的帐户进行身份验证,这对我来说不是一个可以接受的解决方案。不过,这让我切换了默认浏览器,这解决了我最初的问题。