使用 Modern Auth 在 Powershell 中同时连接到合规性和 Exchange 在线

使用 Modern Auth 在 Powershell 中同时连接到合规性和 Exchange 在线

在基本身份验证领域,我曾经按照如下方式连接 MSOL、Compliance 和 Exchange:

        function ConnectToCloud()
        {
            $CloudCredentials = import-clixml C:\tools\CloudCreds.xml
            Write-Host "Connecting To Compliance Online..." -foregroundcolor white -BackgroundColor Green
            $Session1 = New-PSSession -Name "Session1" -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid `
                            -Credential $CloudCredentials -Authentication Basic -AllowRedirection -WarningAction SilentlyContinue
            Import-PSSession $Session1 -Prefix CP -DisableNameChecking -AllowClobber | Out-Null
            Write-Host "Connecting To Exchange Online..." -foregroundcolor white -BackgroundColor Green
            $Session2 = New-PSSession -Name "Session2" -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell `
                            -Credential $CloudCredentials -AllowRedirection -WarningAction SilentlyContinue
            #Import-PSSession $Session2 -Prefix Cloud -DisableNameChecking -AllowClobber | Out-Null
            Connect-MsolService -Credential $CloudCredentials
            Write-Host "Starting the Checks..." -foregroundcolor white -BackgroundColor Green
        }

在现代身份验证的世界中,我们应该将合规性与以下方面联系起来:

    Connect-IPPSSession -Credential $CloudCredentials

并使用以下方式连接到 Exchange:

    Connect-ExchangeOnline -Credential $CloudCredentials

与 MSOL 的连接没有改变。

问题是,当我运行 Connect-ExchangeOnline 时,它​​会断开我与合规性的连接,反之亦然。我如何在脚本中使用现代身份验证同时连接到所有三个服务?

谢谢!

答案1

似乎我使用的是“预览“在线连接模块。

如果我使用你通过Exchange Online 参考,则以下命令可同时连接到合规性和 Exchange Online

        $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
        Import-Module "$MFAExchangeModule"
        $CloudCredentials = import-clixml C:\tools\CloudCreds.xml
        Write-Host "Connecting To Compliance Online..." -foregroundcolor white -BackgroundColor Green
        Connect-IPPSSession -Credential $CloudCredentials -WarningAction SilentlyContinue
        Write-Host "Connecting To Exchange Online..." -foregroundcolor white -BackgroundColor Green
        Connect-ExchangeOnline -Credential $CloudCredentials -ShowBanner:$false
        Connect-MsolService -Credential $CloudCredentials
        Write-Host "Starting the Checks..." -foregroundcolor white -BackgroundColor Green

注意-我无法使用,New-EXOPSSession因为它不允许我给它一个 Credential 参数...交互式登录在脚本中不能很好地工作:(

答案2

我很高兴知道问题已解决,请将有用的回复标记为答案,这将使在论坛中搜索答案变得更容易,并且也使其他社区成员受益。

相关内容