【Azure 環境】使用Microsoft Graph PS SDK 登錄到中國區Azure, 命令Connect-MgGraph -Environment China xxxxxxxxx 遇見登錄錯誤
- 2022 年 4 月 23 日
- 筆記
- 【Azure 環境】, Azure 環境, Microsoft Graph PowerShell SDK, powershell
問題描述
通過PowerShell 連接到Microsoft Graph 中國區Azure,一直出現AADSTS700016錯誤, 消息顯示 the specific application was not installed in your tenant.
解決辦法
根據彈出框中的錯誤提示, 這是因為MS Graph PowerShell並滅有註冊到中國區Azure環境中,所以它無法找到Applicaiton ID: ’14d82eec-204b-4c2f-b7e8-296a70dab67e’。
可以使用以下腳本進行驗證:
Get-AzureADApplication -Filter "AppId eq '14d82eec-204b-4c2f-b7e8-296a70dab67e'"
為了解決這個問題,需要自己在Azure AD中主動註冊一個Application,然後使用這個Application Id進行相應操作。需要執行的操作有:
1)登錄到Azure AD 頁面,註冊一個App (Registered App: //portal.azure.cn/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps)
2)賦予這個App相應的操作許可權,如 User.Read.All, Group.ReadWrite.All 等
3)生成一個證書,並上傳到App中
# 生成自簽名證書 $Cert = New-SelfSignedCertificate -DnsName icy-ps-sdk-aad -CertStoreLocation "Cert:\CurrentUser\My" -FriendlyName "MicrosoftGraphSDK" -Subject "Test Cert for Microsoft Graph SDK"
4)在本地也安裝同樣的證書。
完成以上四步後,就可以使用以下的程式碼登錄中國區Graph了。
#需要選擇從本地選擇證書,完成登錄 Connect-MgGraph -Scopes "User.Read.All" -ClientId "你的appid=clientid" -Environment China -UseDeviceAuthentication # 直接登錄方式 Connect-MgGraph -Environment China -AppId "你的App ID" -TenantId "你的Tenant ID" -CertificateThumbprint "證書指紋 Thumbprint" -Debug
附錄: 完整的腳本
# 生成證書文件 $Cert = New-SelfSignedCertificate -DnsName icy-ps-sdk-aad -CertStoreLocation "Cert:\CurrentUser\My" -FriendlyName "MicrosoftGraphSDK" -Subject "Test Cert for Microsoft Graph SDK" Get-ChildItem "Cert:\CurrentUser\My\$($Cert.thumbprint)" Get-ChildItem "Cert:\CurrentUser\My\$($Cert.thumbprint)" | Export-Certificate -FilePath C:\temp\MicrosoftGraphSDK.cer #登錄 Connect-MgGraph -Scopes "User.Read.All" -ClientId "your clientid" -Environment China -TenantId 「your tenant id」 -UseDeviceAuthentication Connect-MgGraph -Scopes "User.Read.All" -ClientId "your clientid" -Environment China -TenantId 「your tenant id」 -CertificateThumbprint "certificate thumbprint" #獲取User Get-MgUser get-mgcontext #退出登錄 Disconnect-MgGraph
參考資料
Get started with the Microsoft Graph PowerShell SDK://docs.microsoft.com/en-us/powershell/microsoftgraph/get-started?view=graph-powershell-beta