azureazure-active-directoryazure-powershellazure-automation

Unable to list applications from Azure Active Directory using Powershell in Azure Automation Account


I have created an Automation Account runbook with Powershell 5.1 . I have a service principal with the following permissions linked to it using a connection called "local"

enter image description here

My code is as follows :

    $connection = Get-AutomationConnection -Name "local"
    try {
    Write-Output 'Connecting to services'
    Connect-AzureAD -TenantId $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint | Out-null
}
catch {
    Write-Error -Message $_.Exception.Message
    Disconnect-AzureAD | Out-null
    Break
}

Write-Output "Connect process done"
# Function
try {
    Write-Output 'List all apps'
    $list = Get-AzureADApplication -All $true
 $true
    Write-Output $list
}
Catch {
    Write-Error -Message $_.Exception.Message
    Disconnect-AzureAD
}

When I run the code I am getting the following error :

enter image description here

Error occurred while executing GetApplications Code: Authorization_RequestDenied Message: Insufficient privileges to complete the operation. GMT HttpStatusCode: Forbidden HttpStatusDescription: Forbidden HttpResponseStatus: Completed + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

I am not sure why my permission is not working, how do I resolve this ?

Edit: I granted the service principal Directory Read permission but I am getting a new error. Permissions: enter image description here

Error: enter image description here

Error occurred while executing GetApplications Code: Authorization_RequestDenied Message: Insufficient privileges to complete the operation. HttpStatusCode: Forbidden HttpStatusDescription: Forbidden HttpResponseStatus: Completed + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

Solution

  • Unable to list applications from Azure Active Directory using PowerShell in Azure Automation Account.

    When I attempted to run the PowerShell script in Automation account with the 'Application.Read.All' permission, I encountered the same error

    Import-Module AzureADPreview
    Connect-AzureAD -TenantId "TenantId" -ApplicationId "Application-ID" -CertificateThumbprint "849EAFE0fhgjgjgjgggg7340B789221D0E"
    $list = Get-AzureADApplication
    $list.DisplayName
     
    

    Response:

    enter image description here

    To resolve the issue, I assigned the Azure AD role of Directory Readers to my Service Principal and ran the Automation account, expecting the desired result.

    Directory Readers

    enter image description here

    Assigned Directory Readers role to Service Principal

    enter image description here

    Once I assigned the role to my service principal, I got the expected result

    enter image description here

    Reference: Azure AD Graph Retirement and Powershell Module Deprecation