我正在更新我们的PowerShell脚本,以使用更安全的连接方法。当我尝试时,我会发现一个错误,上面写着"UnAuthorized“
Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.0.0\netFramework\ExchangeOnlineManagement.psm1:730连接-
PS X:>连接
$_.Exception;
我在下面的屏幕截图中突出显示的是我应该用于组织参数的内容吗?剪辑
如何修复UnAuthorized错误?
谢谢
发布于 2022-10-27 08:08:54
我同意@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应用程序,如下所示:

对于$organization参数,需要传递您的域名。你可以在这里找到:
转到Azure Portal -> Azure Active Directory ->概述->主域

当我运行下面的脚本连接Exchange 时,得到了如下所示的错误:
$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 Portal -> Azure Active Directory ->角色和管理员-> Exchange管理员->添加赋值

可能需要几分钟才能成功地分配角色,如下所示:

现在,我再次运行脚本连接到Exchange ,并运行示例命令Get-EXOMailbox -PropertySets Archive来验证它,并成功地获得了response,如下所示:
$clientId="47xxxd8-8x2x-4xxx-bxx7-30cxxxxx8"
$thumbPrint="E4A0F6C6B85EBFxxxxxCD91B5803F88E5"
$organization="xxxxxxxx.onmicrosoft.com"
Connect-ExchangeOnline -AppId $clientId -CertificateThumbprint $thumbPrint -Organization $organization输出:

因此,请确保为应用程序指定修复错误所需的角色和权限。
https://stackoverflow.com/questions/74213208
复制相似问题