question

AzDan avatar image
AzDan asked ·

Exporting azure policy definitions

Hi,

I am trying to find a script to export all existing policy definitions that are configured in our environment. I am hoping to export them into a json format that I can then place into source control for CI/CD.

Does anybody know if this exists? Or if not could anyone point me in the right direction for how to achieve this?

Thanks

azure-policy
10 |1000 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.

DCtheGeek avatar image
DCtheGeek answered ·

Each of the SDK can export policy definitions. To get it into JSON from PowerShell for example, you could export them with Get-AzPolicyDefinition and then something like:

$object[0].Properties | ConvertTo-Json
1 comment Share
10 |1000 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.

Hi,

Thanks, this is what I was attempting, the issue I was experiencing was that the nested properties weren't showing correctly, i've resolved this by using

 $object[0].Properties | ConvertTo-Json -depth xx
1 Vote 1 · ·
vasudevareddy-0616 avatar image
vasudevareddy-0616 answered ·

You can export as below using azure CLI

Policy export definition

$policies = az policy definition list | Out-File 'c:\xxxx\policy_definition.json'

from the above file you can also pick type of policies you want

$policies = 'c:\xxxx\policy_definition.json" | ConvertFrom-Json
$final = ($policies | Where-Object {$_.policyType -eq 'Custom'}) | ConvertTo-Json
$final | Out-File ''c:\xxxx\custompolicy_definations.json'


Share
10 |1000 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.