我正在努力更新我們的 PowerShell 腳本以使用更安全的連接方法。當我嘗試時,我收到一條錯誤訊息,顯示“未經授權”
PS X:> Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization UnAuthorized At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.0.0\netFramework\ExchangeOnlineManagement.psm1:730 char:21
- 拋出 $_.Exception;
- CategoryInfo : OperationStopped: (:) [], UnauthorizedAccessException
- FullyQualifiedErrorId : 未授權
我在以下螢屏截圖中突出顯示的內容是我應該用于組織引數的嗎?[剪輯]
如何修復未經授權的錯誤?
謝謝
uj5u.com熱心網友回復:
我同意@scottwtang ,如果您的應用程式沒有所需的角色和權限,您將收到未經授權的錯誤。
我試圖在我的環境中重現相同的結果并得到以下結果:
我使用了您之前問題中的以下腳本來生成證書:
$CN = "GraphApp"
$cert=New-SelfSignedCertificate -Subject "CN=$CN" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature -NotAfter (Get-Date).AddYears(5)
$Thumbprint = $Cert.Thumbprint
Get-ChildItem Cert:\CurrentUser\my\$Thumbprint | Export-Certificate -FilePath $env:USERPROFILE\Downloads\GraphApp.cer
Write-Output "$Thumbprint <- Copy/paste this (save it)"
輸出:

現在我將此證書上傳到 Azure AD 應用程式,如下所示:

對于$organization引數,您需要傳遞您的域名。你可以在這里找到:
轉到 Azure 門戶 -> Azure Active Directory -> 概述 -> 主域

當我運行以下腳本連接Exchange Online時,Access denied出現如下錯誤:
$clientId="47xxxd8-8x2x-4xxx-bxx7-30cxxxxx8"
$thumbPrint="E4A0F6C6B85EBFxxxxxCD91B5803F88E5"
$organization="xxxxxxxx.onmicrosoft.com"
Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization
輸出:

要解決該錯誤,您需要在應用程式中添加角色API permission:Directory

確保為添加的權限授予管理員同意,如下所示:

現在我將Exchange 管理員角色添加到我的應用程式中,如下所示:
轉到 Azure 門戶 -> Azure Active Directory -> 角色和管理員 -> Exchange 管理員 -> 添加分配

成功分配角色可能需要幾分鐘時間,如下所示:

現在我通過再次運行腳本連接到Exchange Online并運行示例命令 Get-EXOMailbox -PropertySets Archive來驗證它并成功獲得回應,如下所示:
$clientId="47xxxd8-8x2x-4xxx-bxx7-30cxxxxx8"
$thumbPrint="E4A0F6C6B85EBFxxxxxCD91B5803F88E5"
$organization="xxxxxxxx.onmicrosoft.com"
Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization
輸出:

因此,請確保為您的應用程式分配所需的角色和權限以修復錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521957.html
標籤:电源外壳天蓝色活动目录
上一篇:如何在根目錄下列出命名空間?
