@@ -50,36 +50,27 @@ try {
50
50
}
51
51
catch {}
52
52
53
- New-Item - Path " $DefinitionsRootFolder \policyDefinitions" - ItemType Directory - Force - ErrorAction SilentlyContinue
54
- New-Item - Path " $DefinitionsRootFolder \policyDefinitions\$Type " - ItemType Directory - Force - ErrorAction SilentlyContinue
55
- New-Item - Path " $DefinitionsRootFolder \policySetDefinitions" - ItemType Directory - Force - ErrorAction SilentlyContinue
56
- New-Item - Path " $DefinitionsRootFolder \policySetDefinitions\$Type " - ItemType Directory - Force - ErrorAction SilentlyContinue
57
- New-Item - Path " $DefinitionsRootFolder \policyAssignments" - ItemType Directory - Force - ErrorAction SilentlyContinue
58
- New-Item - Path " $DefinitionsRootFolder \policyAssignments\$Type " - ItemType Directory - Force - ErrorAction SilentlyContinue
59
-
60
53
# Create policy definition objects
61
54
62
- foreach ($file in Get-ChildItem - Path " $LibraryPath \ platform\ $ ( $Type.ToLower ()) \ policy_definitions" - Recurse - File - Include * .json) {
55
+ foreach ($file in Get-ChildItem - Path " $LibraryPath / platform/ $ ( $Type.ToLower ()) / policy_definitions" - Recurse - File - Include * .json) {
63
56
$fileContent = Get-Content - Path $file.FullName - Raw | ConvertFrom-Json
64
- $baseTemplate = @ {
57
+ $baseTemplate = [ordered ]@ {
58
+ ' $schema' = " https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-definition-schema.json"
65
59
name = $fileContent.name
66
60
properties = $fileContent.properties
67
61
}
68
62
$category = $baseTemplate.properties.Metadata.category
69
- if (! (Test-Path $DefinitionsRootFolder \policyDefinitions\$Type \$category )) {
70
- New-Item - Path $DefinitionsRootFolder \policyDefinitions\$Type \$category - ItemType Directory - Force - ErrorAction SilentlyContinue
71
- }
72
- $baseTemplate | Select-Object name, properties | ConvertTo-Json - Depth 50 | Out-File - FilePath $DefinitionsRootFolder \policyDefinitions\$Type \$category \$ ($fileContent.name ).json - Force
73
- (Get-Content $DefinitionsRootFolder \policyDefinitions\$Type \$category \$ ($fileContent.name ).json) -replace " \[\[" , " [" | Set-Content $DefinitionsRootFolder \policyDefinitions\$Type \$category \$ ($fileContent.name ).json
63
+ ([PSCustomObject ]$baseTemplate | Select-Object - Property " `$ schema" , name, properties | ConvertTo-Json - Depth 50 ) -replace " \[\[" , " [" | New-Item - Path $DefinitionsRootFolder \policyDefinitions\$Type \$category - ItemType File - Name " $ ( $fileContent.name ) .json" - Force - ErrorAction SilentlyContinue
74
64
}
75
65
76
66
# Create policy set definition objects
77
67
78
68
foreach ($file in Get-ChildItem - Path " $LibraryPath \platform\$ ( $Type.ToLower ()) \policy_set_definitions" - Recurse - File - Include * .json) {
79
69
$fileContent = Get-Content - Path $file.FullName - Raw | ConvertFrom-Json
80
- $baseTemplate = @ {
70
+ $baseTemplate = [ordered ]@ {
71
+ " `$ schema" = " https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-set-definition-schema.json"
81
72
name = $fileContent.name
82
- properties = @ {
73
+ properties = [ ordered ] @ {
83
74
description = $fileContent.properties.description
84
75
displayName = $fileContent.properties.displayName
85
76
metadata = $fileContent.properties.metadata
@@ -91,13 +82,13 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
91
82
$policyDefinitions = @ ()
92
83
# Fix the policyDefinitionIds for custom policies
93
84
foreach ($policyDefinition in $fileContent.properties.policyDefinitions ) {
94
- $obj = @ {
85
+ $obj = [ ordered ] @ {
95
86
parameters = $policyDefinition.parameters
96
87
groupNames = $policyDefinition.groupNames
97
88
policyDefinitionReferenceId = $policyDefinition.policyDefinitionReferenceId
98
89
}
99
90
if ($policyDefinition.policyDefinitionId -match " managementGroups" ) {
100
- $obj.Add (" policyDefinitionName" , $policyDefinition.policyDefinitionId.split (" /" )[- 1 ])
91
+ $obj.Add (" policyDefinitionName" , $policyDefinition.policyDefinitionId.split (" /" )[ - 1 ])
101
92
}
102
93
else {
103
94
$obj.Add (" policyDefinitionId" , $policyDefinition.policyDefinitionId )
@@ -107,14 +98,10 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
107
98
$baseTemplate.properties.policyDefinitions = $policyDefinitions
108
99
109
100
$category = $baseTemplate.properties.Metadata.category
110
- if (! (Test-Path $DefinitionsRootFolder \policySetDefinitions\$Type \$category )) {
111
- New-Item - Path $DefinitionsRootFolder \policySetDefinitions\$Type \$category - ItemType Directory - Force - ErrorAction SilentlyContinue
112
- }
113
- $baseTemplate | Select-Object name, properties | ConvertTo-Json - Depth 50 | Out-File - FilePath $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json - Force
114
- (Get-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json) -replace " \[\[" , " [" | Set-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json
115
- (Get-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json) -replace " variables\('scope'\)" , " '/providers/Microsoft.Management/managementGroups/$managementGroupId '" | Set-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json
116
- (Get-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json) -replace " ', '" , " " | Set-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json
117
- (Get-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json) -replace " \[concat\(('(.+)')\)\]" , " `$ 2" | Set-Content $DefinitionsRootFolder \policySetDefinitions\$Type \$category \$ ($fileContent.name ).json
101
+ ([PSCustomObject ]$baseTemplate | Select-Object - Property " `$ schema" , name, properties | ConvertTo-Json - Depth 50 ) -replace " \[\[" , " [" `
102
+ -replace " variables\('scope'\)" , " '/providers/Microsoft.Management/managementGroups/$managementGroupId '" `
103
+ -replace " ', '" , " " `
104
+ -replace " \[concat\(('(.+)')\)\]" , " `$ 2" | New-Item - Path $DefinitionsRootFolder \policySetDefinitions\$Type \$category - ItemType File - Name " $ ( $fileContent.name ) .json" - Force - ErrorAction SilentlyContinue
118
105
}
119
106
120
107
# Create assignment objects
@@ -141,18 +128,18 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
141
128
}
142
129
143
130
144
- $baseTemplate = @ {
131
+ $baseTemplate = [ ordered ] @ {
145
132
" `$ schema" = " https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-assignment-schema.json"
146
133
nodeName = " $ ( $archetypeContent.name ) /$ ( $fileContent.name ) "
147
- assignment = @ {
134
+ assignment = [ ordered ] @ {
148
135
name = $fileContent.Name
149
136
displayName = $fileContent.properties.displayName
150
137
description = $fileContent.properties.description
151
138
}
152
- definitionEntry = @ {
139
+ definitionEntry = [ ordered ] @ {
153
140
displayName = $fileContent.properties.displayName
154
141
}
155
- parameters = @ {}
142
+ parameters = [ ordered ] @ {}
156
143
enforcementMode = $structureFile.enforcementMode
157
144
}
158
145
@@ -163,10 +150,10 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
163
150
164
151
# Definition Entry
165
152
if ($fileContent.properties.policyDefinitionId -match " placeholder.+policySetDefinition" ) {
166
- $baseTemplate.definitionEntry.Add (" policySetName" , ($fileContent.properties.policyDefinitionId ).Split(" /" )[- 1 ])
153
+ $baseTemplate.definitionEntry.Add (" policySetName" , ($fileContent.properties.policyDefinitionId ).Split(" /" )[ - 1 ])
167
154
}
168
155
elseif ($fileContent.properties.policyDefinitionId -match " placeholder.+policyDefinition" ) {
169
- $baseTemplate.definitionEntry.Add (" policyName" , ($fileContent.properties.policyDefinitionId ).Split(" /" )[- 1 ])
156
+ $baseTemplate.definitionEntry.Add (" policyName" , ($fileContent.properties.policyDefinitionId ).Split(" /" )[ - 1 ])
170
157
}
171
158
else {
172
159
if ($fileContent.properties.policyDefinitionId -match " policySetDefinitions" ) {
@@ -186,7 +173,7 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
186
173
if ($scopeTrim -eq " landing_zones" ) {
187
174
$scopeTrim = " landingzones"
188
175
}
189
- $scope = @ {
176
+ $scope = [ ordered ] @ {
190
177
$PacEnvironmentSelector = @ (
191
178
$structureFile.managementGroupNameMappings .$scopeTrim.value
192
179
)
@@ -233,11 +220,7 @@ foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\
233
220
234
221
235
222
$category = $structureFile.managementGroupNameMappings .$scopeTrim.management_group_function
236
- if (! (Test-Path $DefinitionsRootFolder \policyAssignments\$Type \$category )) {
237
- New-Item - Path $DefinitionsRootFolder \policyAssignments\$Type \$category - ItemType Directory - Force - ErrorAction SilentlyContinue
238
- }
239
- $baseTemplate | Select-Object " `$ schema" , nodeName, assignment, definitionEntry, definitionVersion, enforcementMode, parameters, nonComplianceMessages, scope | ConvertTo-Json - Depth 50 | Out-File - FilePath $DefinitionsRootFolder \policyAssignments\$Type \$category \$ ($fileContent.name ).json - Force
240
- (Get-Content $DefinitionsRootFolder \policyAssignments\$Type \$category \$ ($fileContent.name ).json) -replace " \[\[" , " [" | Set-Content $DefinitionsRootFolder \policyAssignments\$Type \$category \$ ($fileContent.name ).json
223
+ ([PSCustomObject ]$baseTemplate | Select-Object - Property " `$ schema" , nodeName, assignment, definitionEntry, definitionVersion, enforcementMode, parameters, nonComplianceMessages, scope | ConvertTo-Json - Depth 50 ) -replace " \[\[" , " [" | New-Item - Path $DefinitionsRootFolder \policyAssignments\$Type \$category - ItemType File - Name " $ ( $fileContent.name ) .json" - Force - ErrorAction SilentlyContinue
241
224
if ($fileContent.name -eq " Deploy-Private-DNS-Zones" ) {
242
225
(Get-Content $DefinitionsRootFolder \policyAssignments\$Type \$category \$ ($fileContent.name ).json) -replace " \.ne\." , " .$dnsZoneRegion ." | Set-Content $DefinitionsRootFolder \policyAssignments\$Type \$category \$ ($fileContent.name ).json
243
226
}
0 commit comments