AzureAD PowerShell – Code: Authentication_Unauthorized

After connecting to your Azure AD using the Connect-AzureAD cmdlet, you will try to retrieve information about your Azure Active Directory, but you may get the following error:

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

Error

Fix

This error means that you are connecting to Azure AD without privileges or you are conencting to the wrong Tenant ID.

PS C:\WINDOWS\system32> Connect-AzureAD

Account               Environment TenantId                             TenantDomain AccountType
-------               ----------- --------                             ------------ -----------
xxxxxxxxxxxxxxxxxxxxx AzureCloud  YYYYYYYYYYYYYYYYYYYYYYY              User

In this case, TenantDomain is “User” which is wrong. To ensure connection to the expected Azure AD Domain, the tenant ID must specified:

PS C:\WINDOWS\system32> Connect-AzureAD -TenantId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Account               Environment TenantId                             TenantDomain                    AccountType
-------               ----------- --------                             ------------                    -----------
yyyyyyyyy@yyyyy.yyy   AzureCloud  xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx yourdomain.onmicrosoft.com      User

Note: The tenant ID can be retrieved using the Get-AzureRmContext cmdlet.

Now you can run the Get-AzureADDomain cmdlet and you will get all the information about your Azure AD.

 

Thanks for reading! You can follow me on Twitter @PrigentNico

About Nicolas 276 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.