question

AnkitaRaniPatro-9753 avatar image
0 Votes"
AnkitaRaniPatro-9753 asked AnkitaRaniPatro-9753 commented

azure policy exemption list using powershell/cli t

My client has 200 azure policy exemption applied to different resource group. I am looking for a powershell command to list all per scope. But I see Get-AzPolicyExemption is not listing out all policies . It only lists the current subscription. But I need all list.

I need help to get a list of azure policy exemption list.









azure-policy
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SudiptaChakraborty-1767 avatar image
0 Votes"
SudiptaChakraborty-1767 answered AnkitaRaniPatro-9753 commented

anonymous user :

You can try out the following powershell script to get the desired results:

 $SubscriptionList = Get-AzSubscription
 $collectionWithItems = New-Object System.Collections.ArrayList
    
 Foreach ($subscription in $SubscriptionList)
 {
     Write-Host $subscription.Name
     Set-AzContext -SubscriptionId $subscription.Id
     $ResourceGroupList = Get-AzResourceGroup    
    
     Foreach ($ResourceGroup in $ResourceGroupList)
     {
         Write-Host 'Resource Id : ' $ResourceGroup.ResourceId
         $exemptions= Get-AzPolicyExemption -Scope $ResourceGroup.ResourceId
    
         $temp = New-Object System.Object
         $temp | Add-Member -MemberType NoteProperty -Name "ResourceId" -Value $ResourceGroup.ResourceId        
    
         Foreach ($exemption in $exemptions)
         {
             Write-Host 'Exemption : ' $exemption.Name
             $temp | Add-Member -MemberType NoteProperty -Name "Exemption" -Value $exemption.Name
    
             $collectionWithItems.Add($temp)
         }
     }
 }
    
    
 $collectionWithItems | Export-Csv -Path .\ExportedData.csv -NoTypeInformation

Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SudiptaChakraborty-1767 , Thanks for the query. I am new to powershell. I am not sure how to export the output to csv. Can you please help me

0 Votes 0 ·

@AnkitaRaniPatro-9753 : I have updated the code provided in the answer with export to CSV functionality in place. Could you please check and let me know in case of any issue.

Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

0 Votes 0 ·
AnkitaRaniPatro-9753 avatar image AnkitaRaniPatro-9753 SudiptaChakraborty-1767 ·

206240-image.png
@SudiptaChakraborty-1767 Thank u so much .I am expecting to see all the results in the azure portal screenshot but I am getting duplicate exemption resource id instead of displayname.


206230-image.png


0 Votes 0 ·
image.png (88.8 KiB)
image.png (30.8 KiB)
Show more comments