【问题标题】:Authenticating with Azure Active Directory on powershell在 Powershell 上使用 Azure Active Directory 进行身份验证
【发布时间】:2017-04-08 23:00:53
【问题描述】:

我正在尝试探索Azure Active Directory V2 PowerShell Module的功能

我有一个 Azure 帐户,并且我设置了一个包含多个用户的 Active Directory。

我的第一个目标很简单:显示用户列表。

所以我输入:

Connect-AzureAD

我会看到一个对话框并输入我的用户帐户和密码。它返回 Microsoft.Open.Azure.AD.CommonLibrary.PSAzureContext 类型的对象。

然后我输入

Get-AzureADUser

错误是:

Get-AzureADUser : 执行 GetUsers 时出错

代码:Authentication_Unauthorized

消息:未找到用户

HttpStatusCode:禁止

我仍然能够列出使用 Azure RM Powershell 模块的用户。以下代码有效:

Add-AzureRmAccount
Get-AzureRmADUser

我该怎么做才能让Get-AzureADUser 工作?

【问题讨论】:

  • 没什么特别的。我建议使用 %localappdata%\Microsoft\AzureAD\Powershell 中与您尝试列出用户相关的最新日志中的详细信息提出支持票证。我们支持查看后端日志以了解导致错误的原因。
  • 你在使用 ARM 吗?

标签: powershell azure azure-active-directory azure-powershell


【解决方案1】:

cmdlet Connect-AzureAD 建立与ADD domian 的连接,登录成功后会显示确认信息:

PS C:\windows\system32> connect-azuread

Account                                Environment Tenant
-------                                ----------- ------
jasontest1@xxxxxx.onmicrosoft.com AzureCloud  xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

可以使用此 cmdlet Get-AzureADDomain 验证连接,如果用户连接到具有管理权限的 AAD 域 - 将显示有关域的信息:

PS C:\windows\system32> get-azureaddomain

Name                                        AvailabilityStatus AuthenticationType
----                                        ------------------ ------------------
hcl.com                                                        Managed
msgamestudios.com                                              Managed
foobar.local                                                   Managed
multimap.com                                                   Managed
skypestaytogether.com                                          Managed
insightsquarterly.com.au                                       Managed
calanit.onmicrosoft.com                                        Federated
msft.ccsctp.net                                                Managed
ruffiangames.com                                               Managed
xn--m1bg0b0byewac1j8b.com                                      Managed
VoicesforInnovation.org                                        Managed
shaanximic.com                                                 Managed
www.yunnanmic.com                                              Managed
wsmbela.pss.com                                                Managed
fornax.off                                                     Managed
api.staging.yammer.com                                         Managed
codenauts.net                                                  Managed
acompli.com                                                    Managed
testdomains.co                                                 Managed
microsoft.hr                                                   Managed
Bayportali.mmdservice.com                                      Managed
contoso.com                                                    Managed
api.swrepository.com                                           Managed
Equivio.com                                                    Managed
sunshine.am                                                    Managed
microsoftaffiliates.com                                        Managed

如果用户有没有管理员权限,我们会得到和你一样的错误。

Get-AzureADDomain : Error occurred while executing GetDomains
Code: Authentication_Unauthorized
Message: User was not found
HttpStatusCode: Forbidden

原因是,GetAzureADDomian cmdlet 没有指定租户,因此与用户没有管理员权限的域建立了连接。

为确保连接到预期的 AAD 域,租户 ID 必须在调用 Connect-AzureAD cmdlet 时指定。

PS C:\windows\system32> Connect-AzureAD -TenantId

【讨论】:

  • 纯金!如果微软现在制作实际的文档,而不是一堆“快速入门”的单页教程,那就太好了。
  • 你成就了我的一天.. :)
【解决方案2】:

这里已经回答了,请使用:

PS C:\windows\system32> Connect-AzureAD -TenantId {YOUR_TENANT_ID}
Example:
PS C:\windows\system32> Connect-AzureAD -TenantId ce1af0ab-ae35-4f60-8f2d-944444444444

使用我们在执行Connect-AzureAd时得到的TenantId是一个常见的错误,如下所示:

但是使用来自Azure Portal --> Azure Active Directory --> Properties --> Directory IdTenantId

Directory Id = TenantId

【讨论】:

  • 这应该是 2020 年以后公认的答案。谢谢! +1
最近更新 更多