在PowerShell中使用Connect-ExchangeOnline命令时出现新的ExopSession错误

在PowerShell中使用Connect-ExchangeOnline命令时出现新的ExopSession错误,powershell,outlook,scripting,exchange-server,Powershell,Outlook,Scripting,Exchange Server,我正在尝试使用connect ExchangeOnline命令连接到PowerShell,但收到以下错误。有什么想法吗 New-ExoPSSession : . At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\1.0.1\ExchangeOnlineManagement.psm1:445 char:30 + ... PSSession = New-ExoPSSession -ExchangeEnvir

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

New-ExoPSSession : . 
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\1.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

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

如果您使用的是非交互式的,那么您需要查找如何以及如何保存这些存储的凭据,用您的配置替换
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 ---
#>
#需要:.Net 4.5、Windows管理框架5.1(请参阅https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps)
#
#运行一次:
#设置ExecutionPolicy远程签名;获取执行策略
#安装模块PowerShellGet-强制
#安装模块–名称ExchangeOnlineManagement
#
# https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/exchange-online-powershell-v2/exchange-online-powershell-v2?view=exchange-ps
$exo=新模块-AsCustomObject-ScriptBlock{
$UserName=“默认值。Username@domain"
$UserCredential=获取凭据-消息“输入365管理员凭据”-用户名$UserName
函数已连接(){
试一试{
如果(
@($(获取邮箱-结果大小1-警告操作silentlycontinue)).count`
-情商
1.
){返回$true}
}
捕获{}
返回$false
}
函数连接(){
$result=“未完成”
如果($this.IsConnected()){
$result=“成功”
}否则{
$UserCredential=$this.UserCredential
连接ExchangeOnline-凭据$UserCredential
如果($this.IsConnected()){
$result=“成功”
}否则{
$result=“失败”
}
}
切换($result){
“Unfinished”{Write Warning”`n.Connect()中出现未知错误,似乎在未完成时已结束;break}
“成功”{写入主机”`n成功连接到Exchange 365;中断}
“失败”{写入警告”`n连接到Exchange 365失败;中断}
默认值{write warning“`nAn未知错误发生在.Connect()中,结果代码无法识别;break}
}
#旧式
#$Session=New PSSession-ConfigurationName Microsoft.Exchange-ConnectionUrihttps://outlook.office365.com/powershell-liveid/ -凭证$UserCredential-身份验证基本-AllowerDirection
#导入PSSession$会话-DisableNameChecking-AllowClobber
}
函数ConnectMsol(){
连接MsolService-凭据$UserCredential
}
功能断开(){
断开ExchangeOnline
#老赛特
#删除PSSession$会话
}
功能循环(){
$this.Disconnect()
$this.Connect()
}
导出模块成员-函数*-变量*
}
$exo.Connect()
#$exo.ConnectMsol()

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

最新版本的Exchange Online module需要PowerShell V7。我一安装V7,它就工作得很好

Per:

PowerShell 7.0.3或更高版本支持EXO V2模块


通过将Windows 10中的默认web浏览器切换到Chromium Edge浏览器,我可以解决类似的问题。我打开了一次登录窗口,然后将Firefox切换回我的默认浏览器,它仍然工作


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

请显示您输入的凭证格式。通常,凭证用户名是该帐户的
UserPrincipalName
值。只是
userid
不起作用。
SamAccountName
通常不起作用,因为如果它是prem帐户上的广告,
SamAccountName
值与Azure中的值不匹配。说到云账户,
SamAccountName
通常不是您所期望的
UserPrincipalName
(UPN)是可预测的,从prem到Azure(如果处于混合模式)或在云中创建时应该保持一致。UPN格式看起来像电子邮件地址。使用
domain\userid
格式的用户名连接到ExchangeOnline会产生此错误。@AdminOfThings是的,我使用的是domain/userid格式。所以我应该使用我公司的电子邮件,这就是你所说的??为同一个问题创建多个问题没有任何帮助。一般来说,提供帮助的速度非常快。相信我,你的问题不会在这堆问题中消失。如果这些问题没有得到回答,要么过于复杂,无法快速回答,要么你没有提供足够的信息。这是您没有提供足够信息的情况。我无权提出问题,但可以根据您尝试的步骤编辑问题并添加更多信息或提供更新信息。有时,为了找到解决方案,需要不断更新问题的数据。人们通常不喜欢的是期望值超过问题的要求。你也不想在帖子中充斥太多不必要的信息。这是一种平衡,注释有助于定位焦点。这三个命令是独立的,还是三行上只有一个命令?无论我用哪种方式尝试,我都会出错。这些命令应该一个接一个地尝试。所以三个命令结合在一起,希望脚本中清楚,但是这会创建一个$exo对象,您可以使用它调用
$exo。