Powershell

使用 Modern Auth 在 Powershell 中線上連接到合規性和 Exchange

  • May 5, 2020

在基本身份驗證領域,我曾經以如下方式連接到 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
       }

在 Modern Auth 的世界中,我們應該通過以下方式連接合規性:

   Connect-IPPSSession -Credential $CloudCredentials

並通過以下方式連接到 Exchange:

   Connect-ExchangeOnline -Credential $CloudCredentials

與 MSOL 的連接沒有改變。

問題是當我執行 Connect-ExchangeOnline 時,它使我與合規性斷開連接,反之亦然 我如何在腳本中使用現代身份驗證同時連接到所有三個服務?

謝謝!

看來我正在使用線上連接的“預覽”模組。

如果我使用您通過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 參數……互動式登錄在腳本中效果不佳:(

引用自:https://serverfault.com/questions/1015193