question

DiptiRanjanSwain-1959 avatar image
0 Votes"
DiptiRanjanSwain-1959 asked DiptiRanjanSwain-1959 answered

Get list of NonCompliant resources for a management group

I am trying to get list of all non-compliant resources from Azure for a certain management group. For that I am using below Powershell query, but it seems the result is getting limited to 1000 records, so I am not getting all the records. In the management group there are around 9 K non-compliant resources. Is there a way to set pagination or download all the resource records.

$policyEvents = Get-AzPolicyState -ManagementGroupName "Test" -Filter "ComplianceState eq 'NonCompliant'"
$policyEvents | ConvertTo-Csv | Out-File 'mggroup.csv'

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.

tbgangav-MSFT avatar image
0 Votes"
tbgangav-MSFT answered

Hi @DiptiRanjanSwain-1959,

The default limit is set to 1000 so you would have to use "Top" parameter in Get-AzPolicyState command. So your command would look something like shown below. Let me know if it doesn't work.

 $policyEvents = Get-AzPolicyState -ManagementGroupName "CPTO" -Filter "ComplianceState eq 'NonCompliant'" -Top 10000
 $policyEvents | ConvertTo-Csv | Out-File 'mggroup.csv'
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.

DiptiRanjanSwain-1959 avatar image
0 Votes"
DiptiRanjanSwain-1959 answered

Hi @tbgangav-MSFT

Thank you for quickly answering the query. Top does work, but it seems it has limit below 5K. It is not even allowing to specify 5K.

Also there seems to be some bug with Azure query, it returns the same number of records specified by top irrespective of whether more or less records present in the portal, but your answer is correct. Thank you once again.

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.