-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Azure/feature/export
Added export scripts
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<# | ||
.SYNOPSIS | ||
Exports a policy assignment from Azure to a local file in the EPAC format | ||
.DESCRIPTION | ||
Exports a policy assignment from Azure to a local file in the EPAC format | ||
.EXAMPLE | ||
New-EPACPolicyAssignmentDefinition.ps1 -PolicyDefinitionId "/providers/Microsoft.Management/managementGroups/epac/providers/Microsoft.Authorization/policyDefinitions/Append-KV-SoftDelete" -OutputFolder .\ | ||
Export the policy definition to the current folder. | ||
#> | ||
|
||
[CmdletBinding()] | ||
|
||
Param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[string]$PolicyAssignmentId, | ||
[string]$OutputFolder | ||
) | ||
|
||
. "$PSScriptRoot/../Helpers/ConvertTo-HashTable.ps1" | ||
|
||
$PolicyAssignment = Get-AzPolicyAssignment -Id $PolicyAssignmentId | ||
if ($PolicyAssignment) { | ||
if ($PolicyAssignment.Properties.PolicyDefinitionId -match "Microsoft.Authorization/policyDefinitions") { | ||
$baseTemplate = @{ | ||
assignment = @{ | ||
name = $PolicyAssignment.Name | ||
displayName = $PolicyAssignment.Properties.DisplayName | ||
description = $PolicyAssignment.Properties.Description | ||
} | ||
definitionEntry = @{ | ||
policyName = $PolicyAssignment.Properties.PolicyDefinitionId.Split("/")[-1] | ||
} | ||
parameters = @{} | ConvertTo-HashTable | ||
} | ||
($PolicyAssignment.Properties.Parameters | ConvertTo-HashTable).GetEnumerator() | ForEach-Object { | ||
$baseTemplate.parameters.Add($_.Name, $_.Value.Value) | ||
} | ||
if ($OutputFolder) { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | Out-File "$OutputFolder\$($policyAssignment.Name).json" | ||
} | ||
else { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | ||
} | ||
} | ||
if ($PolicyAssignment.Properties.PolicyDefinitionId -match "Microsoft.Authorization/policySetDefinitions") { | ||
$baseTemplate = @{ | ||
assignment = @{ | ||
name = $PolicyAssignment.Name | ||
displayName = $PolicyAssignment.Properties.DisplayName | ||
description = $PolicyAssignment.Properties.Description | ||
} | ||
definitionEntry = @{ | ||
initiativeName = $PolicyAssignment.Properties.PolicyDefinitionId.Split("/")[-1] | ||
} | ||
parameters = @{} | ConvertTo-HashTable | ||
} | ||
($PolicyAssignment.Properties.Parameters | ConvertTo-HashTable).GetEnumerator() | ForEach-Object { | ||
$baseTemplate.parameters.Add($_.Name, $_.Value.Value) | ||
} | ||
if ($OutputFolder) { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | Out-File "$OutputFolder\$($policyAssignment.Name).json" | ||
} | ||
else { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<# | ||
.SYNOPSIS | ||
Exports a policy definition from Azure to a local file in the EPAC format | ||
.DESCRIPTION | ||
Exports a policy definition from Azure to a local file in the EPAC format | ||
.EXAMPLE | ||
New-EPACPolicyDefinition.ps1 -PolicyDefinitionId "/providers/Microsoft.Management/managementGroups/epac/providers/Microsoft.Authorization/policyDefinitions/Append-KV-SoftDelete" -OutputFolder .\ | ||
Export the policy definition to the current folder. | ||
#> | ||
|
||
[CmdletBinding()] | ||
|
||
Param( | ||
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[string]$PolicyDefinitionId, | ||
[string]$OutputFolder | ||
) | ||
|
||
. "$PSScriptRoot/../Helpers/ConvertTo-HashTable.ps1" | ||
|
||
if ($PolicyDefinitionId -match "Microsoft.Authorization/policyDefinitions") { | ||
$policyDefinition = Get-AzPolicyDefinition -Id $PolicyDefinitionId | ||
$baseTemplate = @{ | ||
name = $PolicyDefinition.name | ||
properties = $policyDefinition.Properties | Select-Object Description, DisplayName, Mode, Parameters, PolicyRule, @{n = "Metadata"; e = { $_.Metadata | Select-Object Version, Category } } | ||
} | ||
if ($OutputFolder) { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | Out-File "$OutputFolder\$($policyDefinition.Name).json" | ||
} | ||
else { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | ||
} | ||
} | ||
|
||
if ($PolicyDefinitionId -match "Microsoft.Authorization/policySetDefinitions") { | ||
$policyDefinition = Get-AzPolicySetDefinition -Id $PolicyDefinitionId | ||
$baseTemplate = @{ | ||
name = $PolicyDefinition.Name | ||
properties = $policyDefinition.Properties | Select-Object Description, DisplayName, Mode, PolicyDefinitionGroups, Parameters, PolicyDefinitions, @{n = "Metadata"; e = { $_.Metadata | Select-Object Version, Category } } | ||
} | ||
$baseTemplate.properties.PolicyDefinitions | Foreach-Object { | ||
$_ | Add-Member -Type NoteProperty -Name policyDefinitionName -Value $_.policyDefinitionId.Split("/")[-1] | ||
$_.psObject.Properties.Remove('policyDefinitionId') | ||
} | ||
if ($OutputFolder) { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | Out-File "$OutputFolder\$($policyDefinition.Name).json" | ||
} | ||
else { | ||
$baseTemplate | ConvertTo-Json -Depth 50 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters