Exporting azure policy definitions

Dan 176 Reputation points
2020-06-30T19:14:22.097+00:00

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
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
912 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DCtheGeek-MSFT 451 Reputation points Microsoft Employee
    2020-07-03T02:40:38.137+00:00

    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
    

  2. vasudeva reddy 41 Reputation points
    2020-07-30T08:16:23.797+00:00

    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'

    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.