Identify the Azure Policy which is linked to a Policy Assignment

Chris Pretorius 0 Reputation points
2025-08-21T00:07:19.0566667+00:00

Hi

I am analysing a customer's Azure Policy and noticed that, in this case, there are 2x Policy Definitions, but 34x Policy Assignments.

When analysing each Policy Assignment (using the "View Definition" button at the top of the Assignment ribbon), the name is different (Assignment: "Require a tag on resource groups: "WorkloadName"", View Definition: "Require a tag on resource groups").

I thought each Assignment has a 1:1 relationship with a Definition, allowing the assignment of definitions to different scopes (MG, Sub, etc.).

How can I get an accurate (i.e., not different name, as above) view of the Policy within each Assignment?

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
{count} votes

1 answer

Sort by: Most helpful
  1. Rahul Jorrigala 4,320 Reputation points Microsoft External Staff Moderator
    2025-08-22T16:50:45.58+00:00

    Hello Chris Pretorius

    Please use below updated script to get the policies list at management scope.

    # Login if needed
    Connect-AzAccount
    
    # Initialize array
    $vmArray = @()
    
    # Get all management groups
    $mgList = Get-AzManagementGroup
    
    foreach ($mg in $mgList) {
        $mgName = $mg.Name
        Write-Host "Processing Management Group: $mgName"
    
        # Get non-compliant resources at MG level
        $nonCompliantResources = Get-AzPolicyState -ManagementGroupName $mgName | Where-Object { $_.ComplianceState -eq "NonCompliant" }
    
        Write-Host "Non-Compliant Resources in $mgName: $($nonCompliantResources.Count)"
    
        foreach ($resource in $nonCompliantResources) {
            $resourceName = $resource.resourceId.Split('/')[-1]
            $resourceType = $resource.resourceType
            $complianceState = $resource.complianceState
            $resourceGroup = $resource.resourceGroup
            $resourceLocation = $resource.resourceLocation
            $policyDefinitionName = $resource.PolicyDefinitionReferenceId
            $PolicyAssignmentName = $resource.PolicyAssignmentName
            $InitiativeId = $resource.PolicySetDefinitionId
            $InitiativeName = $resource.PolicySetDefinitionName
            $subscriptionId = $resource.SubscriptionId
    
            # Get Initiative Display Name
            $InitiativeDisplayName = $null
            if ($InitiativeId) {
                $initiativeDetails = Get-AzPolicySetDefinition -Id $InitiativeId -ErrorAction SilentlyContinue
                if ($initiativeDetails) {
                    $InitiativeDisplayName = $initiativeDetails.Properties.DisplayName
                }
            }
    
            # Store details
            $vmArray += New-Object PSObject -Property @{
                ManagementGroupName   = $mgName
                PolicyDefinitionName  = $policyDefinitionName
                InitiativeDisplayName = $InitiativeDisplayName
                ComplianceState       = $complianceState
                SubscriptionId        = $subscriptionId
                ResourceGroup         = $resourceGroup
                ResourceName          = $resourceName
                ResourceType          = $resourceType
                ResourceLocation      = $resourceLocation
            }
        }
    }
    
    # Export to CSV
    $vmArray | Sort-Object ManagementGroupName, PolicyDefinitionName, InitiativeDisplayName, ComplianceState, SubscriptionId, ResourceGroup, ResourceName, ResourceType, ResourceLocation | Export-CSV -Path ".\mg-compliance-all.csv" -NoTypeInformation
    
    
    

    Please let me know if you face any challenge here, I can help you to resolve this issue further

    If the comment was helpful, please click "Upvote"

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.