Hi, you should be able to do this with, a script like the below:
foreach($g in $mgs){
Write-Host "Exporting group: $g"
$mg = Get-AzManagementGroup -GroupName $g -Expand
if($mg.ParentName -ne $tenantId){
$parentMg = $mg.ParentName
}else{
$parentMg = ""
}
# managementGroup parameter value
$managmentGroupParam = [ordered]@{
name = $mg.Name
parentName = $parentMg
displayName = $mg.displayName
}
# subscriptions parameter value
$subscriptionsParam = @()
foreach($c in $mg.Children){
if($c.Type -eq "/subscriptions"){
$subscriptionsParam += $c.Name
}
}
# policy defintions - API returns all of them, even though you ask for scope
Write-Host "Get policy definitions for managementGroup: $g"
$pds = Get-AzPolicyDefinition -Custom -ManagementGroupName $g
$policyDefParam = @()
foreach($pd in $pds){
# see if this definition is actually deployed to this group since the API returns everything above it, if it is, add it to the list
if($pd.resourceId -like "*Microsoft.Management/managementGroups/$g*"){
$policyDefParam += $pd.name
# Get PolicyDef JSON
$json = Get-PolicyDefinitionObject $pd
# Write parameter file
$json | ConvertTo-Json -Depth 20 | Set-Content -Path "$PSScriptRoot/policyDefinitions/$($pd.name).parameters.json"
}
}