【问题标题】:New-ExoPSSession error when using Connect-ExchangeOnline command in PowerShell在 PowerShell 中使用 Connect-ExchangeOnline 命令时出现 New-ExoPSSession 错误
【发布时间】:2021-04-06 07:28:56
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 请显示您输入的凭证的格式。通常,凭据用户名是该帐户的 UserPrincipalName 值。只是userid 是行不通的。 SamAccountName 通常不起作用,因为如果它是 AD 本地帐户,SamAccountName 值与 Azure 中的值不匹配。对于云帐户,SamAccountName 通常不是您所期望的。 UserPrincipalName (UPN) 是可预测的,并且应该从本地到 Azure(如果处于混合模式)或在云中创建保持一致。 UPN 格式看起来像一个电子邮件地址。
  • 使用domain\userid 格式的用户名连接到ExchangeOnline 会产生此错误。
  • @AdminOfThings 是的,我正在使用格式域/用户 ID。所以我应该使用我的公司电子邮件是你说的吗??
  • 为同一个问题创建多个问题没有任何帮助。通常,提供帮助的速度非常快。当我说你的问题不会被淹没时,请相信我。如果他们没有得到答复,它们要么太复杂而无法快速回答,要么您没有提供足够的信息。这是您没有提供足够信息的情况。
  • 我不是提问的权威,但可以根据您尝试的步骤编辑问题并添加更多信息或提供更新信息。有时需要不断演变问题的数据才能找到解决方案。人们通常不喜欢的期望超出问题的要求。您也不想在帖子中充斥太多不必要的信息。有一个平衡点,cmets 帮助定位焦点。

标签: powershell outlook scripting exchange-server


【解决方案1】:

请尝试如下使用cmdlet:

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

【讨论】:

  • 是这三个单独的命令,还是三行中的一个命令?无论我尝试哪种方式,都会出错。
  • 这些命令应该一一尝试。所以三个命令一起
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    如果您使用的是这种非交互式的,您需要查看如何使用 store your credentials securely 以及如何使用 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 ---
    #>
    

    【讨论】:

    • 希望它在脚本中很清楚,但这会创建一个 $exo 对象,您可以使用它调用 $exo.Connect()$exo.Disconnect()$exo.Cycle()$exo.IsConnected()
    • 这太痛苦了。我要做的只是显示一个隐藏在 Exchange 管理控制台中的邮箱,因为我删除了用户的许可证。事实证明你必须使用 Power Shell 来做到这一点(为什么?)然后你必须编写一个脚本来做到这一点?他们能让简单的任务变得更复杂吗?
    • 脚本失败:使用“0”参数调用“Connect”的异常:使用 Oauth 在 logon.ps1:64:1 Exo.Connnect() 创建 Powershell 会话失败
    • 您是否让所有 3 个“运行一次”行都运行并确保您同时拥有 Powershell 5(或更高版本)和 .Net 4.5?如果是这样,那么another user 有类似的问题,必须卸载然后重新安装exchange online powershell 模块
    • 经过一些处理后,似乎有一些 Microsoft Intune 设置引起了 conflict
    【解决方案4】:

    最新版本的 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

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-08-13
        • 2022-10-31
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 2021-12-11
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多