azure-ad-powershell-v2

Authentication_Unauthorized while fetching list of applications in AAD


I am trying to get list of applications registered in AAD via PowerShell.

I got into my tenant successfully using Connect-AzureAD. But when I'm running Get-AzureADApplication, I'm getting error like below:

Get-AzureADApplication : Error occurred while executing GetApplications 
Code: Authentication_Unauthorized
Message: User was not found.
RequestId: 28b83872-c29b-423a-9870-ed2ad714f597
DateTimeStamp: Sat, 25 Jun 2022 1:35:23 GMT
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed
At line:1 char:1
+ Get-AzureADApplication
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-AzureADApplication], ApiException
    + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetApplication

What does User was not found message mean?

I have Global Admin role and I tried elevating my access too that gives me access to all. But still I'm facing the same error.

Can anyone help me out what am I missing here?


Solution

  • The error 'User not found' usually occurs if you are trying to retrieve info of one tenant by connecting wrong(different) tenant or tenant that does not exist.

    Please check the TenantDomain you are getting in the response while executing Connect-AzureAD cmdlet.

    I tried to reproduce the same in my environment and got below results:

    When I ran Connect-AzureAD cmdlet and logged in with personal Microsoft account, I got blank under TenantDomain that means it does not exist.

    I got the same error when I executed Get-AzureADApplication after that like below:

    enter image description here

    To resolve the error, try running Connect-AzAccount first and include TenantId that you got in response while executing Connect-AzureAD cmdlet like below:

    Connect-AzAccount
    Connect-AzureAD -TenantId 'your_tenant_id'
    Get-AzureADApplication
    

    I got the list of Azure AD applications successfully after running above script like below:

    enter image description here

    Reference:

    powershell - Authentication_Unauthorized, User Not Found - answered by JoyWang