Skip to content

Commit b6c2235

Browse files
authored
Merge pull request #206410 from timwarner-msft/timwarner-pshell
Add initiative-level PowerShell example
2 parents dcdc8f3 + aa79b67 commit b6c2235

File tree

1 file changed

+62
-27
lines changed

1 file changed

+62
-27
lines changed

articles/governance/policy/how-to/remediate-resources.md

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Remediate non-compliant resources
33
description: This guide walks you through the remediation of resources that are non-compliant to policies in Azure Policy.
4-
ms.date: 04/27/2022
4+
ms.date: 07/29/2022
55
ms.topic: how-to
66
ms.author: timwarner
77
author: timwarner-msft
@@ -67,7 +67,7 @@ az role definition list --name "Contributor"
6767
6868
## Configure the managed identity
6969

70-
Each Azure Policy assignment can be associated with only one managed identity. However, the managed identity can be assigned multiple roles. Configuration occurs in two steps: first create either a system-assigned or user-assigned managed identity, then grant it the necessary roles.
70+
Each Azure Policy assignment can be associated with only one managed identity. However, the managed identity can be assigned multiple roles. Configuration occurs in two steps: first create either a system-assigned or user-assigned managed identity, then grant it the necessary roles.
7171

7272
> [!NOTE]
7373
> When creating a managed identity through the portal, roles will be granted automatically to the managed identity. If **roleDefinitionIds** are later edited in the policy definition, the new permissions must be manually granted, even in the portal.
@@ -80,25 +80,25 @@ When creating an assignment using the portal, Azure Policy can generate a system
8080

8181
:::image type="content" source="../media/remediate-resources/remediation-tab.png" alt-text="Screenshot of a policy assignment creating a system-assigned managed identity in East US with Log Analytics Contributor permissions.":::
8282

83-
To set a system-assigned managed identity in the portal:
83+
To set a system-assigned managed identity in the portal:
8484

85-
1. On the **Remediation** tab of the create/edit assignment view, under **Types of Managed Identity**, ensure that **System assigned managed identity**
86-
is selected.
85+
1. On the **Remediation** tab of the create/edit assignment view, under **Types of Managed Identity**, ensure that **System assigned managed identity**
86+
is selected.
8787

88-
1. Specify the location at which the managed identity is to be located.
88+
1. Specify the location at which the managed identity is to be located.
8989

90-
To set a user-assigned managed identity in the portal:
90+
To set a user-assigned managed identity in the portal:
9191

92-
1. On the **Remediation** tab of the create/edit assignment view, under **Types of Managed Identity**, ensure that **User assigned managed identity**
93-
is selected.
92+
1. On the **Remediation** tab of the create/edit assignment view, under **Types of Managed Identity**, ensure that **User assigned managed identity**
93+
is selected.
9494

95-
1. Specify the scope where the managed identity is hosted. The scope of the managed identity does not have to equate to the scope of the assignment, but it must be in the same tenant.
95+
1. Specify the scope where the managed identity is hosted. The scope of the managed identity does not have to equate to the scope of the assignment, but it must be in the same tenant.
9696

97-
1. Under **Existing user assigned identities**, select the managed identity.
97+
1. Under **Existing user assigned identities**, select the managed identity.
9898

9999
# [PowerShell](#tab/azure-powershell)
100100

101-
To create an identity during the assignment of the policy, **Location** must be defined and **Identity** used.
101+
To create an identity during the assignment of the policy, **Location** must be defined and **Identity** used.
102102

103103
The following example gets the definition of the built-in policy **Deploy SQL DB transparent data encryption** sets the target resource group, and then creates the assignment using a **system assigned** managed identity.
104104

@@ -129,15 +129,15 @@ $policyDef = Get-AzPolicyDefinition -Id '/providers/Microsoft.Authorization/poli
129129
$resourceGroup = Get-AzResourceGroup -Name 'MyResourceGroup'
130130
131131
# Get the existing user assigned managed identity ID
132-
$userassignedidentity = Get-AzUserAssignedIdentity -ResourceGroupName $rgname -Name $userassignedidentityname
132+
$userassignedidentity = Get-AzUserAssignedIdentity -ResourceGroupName $rgname -Name $userassignedidentityname
133133
$userassignedidentityid = $userassignedidentity.Id
134134
135135
# Create the assignment using the -Location and -Identity properties
136136
$assignment = New-AzPolicyAssignment -Name 'sqlDbTDE' -DisplayName 'Deploy SQL DB transparent data encryption' -Scope $resourceGroup.ResourceId -PolicyDefinition $policyDef -Location 'westus' -IdentityType "UserAssigned" -IdentityId $userassignedidentityid
137137
```
138138

139139
The `$assignment` variable now contains the principal ID of the managed identity along with the standard values returned when creating a policy assignment. It can be accessed through
140-
`$assignment.Identity.PrincipalId` for system-assigned managed identities and `$assignment.Identity.UserAssignedIdentities[$userassignedidentityid].PrincipalId` for user-assigned managed identities.
140+
`$assignment.Identity.PrincipalId` for system-assigned managed identities and `$assignment.Identity.UserAssignedIdentities[$userassignedidentityid].PrincipalId` for user-assigned managed identities.
141141

142142
# [Azure CLI](#tab/azure-cli)
143143

@@ -150,13 +150,13 @@ To add a system-assigned identity or a user-assigned identity to a policy assign
150150
### Grant permissions to the managed identity through defined roles
151151

152152
> [!IMPORTANT]
153-
>
153+
>
154154
> If the managed identity does not have the permissions needed to execute the required remediation task, it will be granted permissions *automatically* only through the portal. You may skip this step if creating a managed identity through the portal.
155155
>
156156
> For all other methods, the assignment's managed identity must be manually granted access through the addition of roles, or else the remediation deployment will fail.
157-
>
157+
>
158158
> Example scenarios that require manual permissions:
159-
> - If the assignment is created through SDK
159+
> - If the assignment is created through an Azure software development kit (SDK)
160160
> - If a resource modified by **deployIfNotExists** or **modify** is outside the scope of the policy
161161
> assignment
162162
> - If the template accesses properties on resources outside the scope of the policy assignment
@@ -200,22 +200,57 @@ To add a role to the assignment's managed identity, follow these steps:
200200
# [PowerShell](#tab/azure-powershell)
201201

202202
The new managed identity must complete replication through Azure Active Directory before it can be
203-
granted the needed roles. Once replication is complete, the following example iterates the policy
203+
granted the needed roles. Once replication is complete, the following examples iterate the policy
204204
definition in `$policyDef` for the **roleDefinitionIds** and uses
205205
[New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment) to
206206
grant the new managed identity the roles.
207207

208+
Specifically, the first example shows you how to grant roles at the policy scope. The second
209+
example demonstrates how to grant roles at the initiative (policy set) scope.
210+
208211
```azurepowershell-interactive
212+
###################################################
213+
# Grant roles to managed identity at policy scope #
214+
###################################################
215+
209216
# Use the $policyDef to get to the roleDefinitionIds array
210217
$roleDefinitionIds = $policyDef.Properties.policyRule.then.details.roleDefinitionIds
211218
212219
if ($roleDefinitionIds.Count -gt 0)
213220
{
214221
$roleDefinitionIds | ForEach-Object {
215222
$roleDefId = $_.Split("/") | Select-Object -Last 1
216-
New-AzRoleAssignment -Scope $resourceGroup.ResourceId -ObjectId $assignment.Identity.PrincipalId -RoleDefinitionId $roleDefId
223+
New-AzRoleAssignment -Scope $resourceGroup.ResourceId -ObjectId $assignment.Identity.PrincipalId
224+
-RoleDefinitionId $roleDefId
217225
}
218226
}
227+
228+
#######################################################
229+
# Grant roles to managed identity at initiative scope #
230+
#######################################################
231+
232+
#If the policy had no managed identity in its logic, then no impact. If there is a managed identity
233+
used for enforcement, replicate it on the new assignment.
234+
$getNewInitiativeAssignment = Get-AzPolicyAssignment -Name $newInitiativeDefinition.Name
235+
236+
#Create an array to store role definition's IDs used by policies inside the initiative.
237+
$InitiativeRoleDefinitionIds = @();
238+
239+
#Loop through the policy definitions inside the initiative and gather their role definition IDs
240+
foreach ($policyDefinitionIdInsideInitiative in $InitiativeDefinition.Properties.PolicyDefinitions.policyDefinitionId) {
241+
$policyDef = Get-AzPolicyDefinition -Id $policyDefinitionIdInsideInitiative
242+
$roleDefinitionIds = $policyDef.Properties.PolicyRule.then.details.roleDefinitionIds
243+
$InitiativeRoleDefinitionIds += $roleDefinitionIds
244+
}
245+
246+
#Create the role assignments used by the initiative assignment at the subscription scope.
247+
if ($InitiativeRoleDefinitionIds.Count -gt 0) {
248+
$InitiativeRoleDefinitionIds | Sort-Object -Unique | ForEach-Object {
249+
$roleDefId = $_.Split("/") | Select-Object -Last 1
250+
New-AzRoleAssignment -Scope "/subscriptions/$($subscription)" -ObjectId $getNewInitiativeAssignment.Identity.PrincipalId
251+
-RoleDefinitionId $roleDefId
252+
}
253+
}
219254
```
220255

221256
# [Azure CLI](#tab/azure-cli)
@@ -246,7 +281,7 @@ There are three ways to create a remediation task through the portal.
246281
1. All **deployIfNotExists** and **modify** policy assignments are
247282
shown on the **Policies to remediate** tab. Select one with resources
248283
that are non-compliant to open the **New remediation task** page.
249-
284+
250285
1. Follow steps to [specify remediation task details](#step-2-specify-remediation-task-details).
251286

252287
#### Option 2: Create a remediation task from a non-compliant policy assignment
@@ -267,7 +302,7 @@ the **Remediation** tab of the wizard offers a _Create a remediation task_ optio
267302
> [!NOTE]
268303
> This is the most streamlined approach for creating a remediation task and is supported for policies assigned on a _subscription_. For policies assigned on a _management group_, remediation tasks should be created using [Option 1](#option-1-create-a-remediation-task-from-the-remediation-page) or [Option 2](#option-2-create-a-remediation-task-from-a-non-compliant-policy-assignment) after evaluation has determined resource compliance.
269304
270-
1. From the assignment wizard in the portal, navigate to the **Remediation** tab. Select the check box for **Create a remediation task**.
305+
1. From the assignment wizard in the portal, navigate to the **Remediation** tab. Select the check box for **Create a remediation task**.
271306

272307
1. If the remediation task is initiated from an initiative assignment, select the policy to remediate from the drop-down.
273308

@@ -277,21 +312,21 @@ the **Remediation** tab of the wizard offers a _Create a remediation task_ optio
277312

278313
This step is only applicable when using [Option 1](#option-1-create-a-remediation-task-from-the-remediation-page) or [Option 2](#option-2-create-a-remediation-task-from-a-non-compliant-policy-assignment) to initiate remediation task creation.
279314

280-
1. If the remediation task is initiated from an initiative assignment, select the policy to remediate from the drop-down. One **deployIfNotExists** or **modify** policy can be remediated through a single Remediation task at a time.
315+
1. If the remediation task is initiated from an initiative assignment, select the policy to remediate from the drop-down. One **deployIfNotExists** or **modify** policy can be remediated through a single Remediation task at a time.
281316

282-
1. Optionally modify remediation settings on the **New remediation task** page:
317+
1. Optionally modify remediation settings on the **New remediation task** page:
283318

284-
- **Failure Threshold percentage** - Used to specify whether the remediation task should fail if the percentage of failures exceeds the given threshold. Provided as a number between 0 to 100. By default, the failure threshold is 100%.
319+
- **Failure Threshold percentage** - Used to specify whether the remediation task should fail if the percentage of failures exceeds the given threshold. Provided as a number between 0 to 100. By default, the failure threshold is 100%.
285320
- **Resource Count** - Determines how many non-compliant resources to remediate in a given remediation task. The default value is 500 (the previous limit). The maximum number is 50,000 resources.
286321
- **Parallel Deployments** - Determines how many resources to remediate at the same time. The allowed values are 1 to 30 resources at a time. The default value is 10.
287322

288323
> [!NOTE]
289324
> These settings cannot be changed once the remediation task has started.
290-
325+
291326
1. On the same page, filter the resources to remediate by using the **Scope**
292327
ellipses to pick child resources from where the policy is assigned (including down to the
293328
individual resource objects). Additionally, use the **Locations** dropdown list to further filter
294-
the resources.
329+
the resources.
295330

296331
:::image type="content" source="../media/remediate-resources/select-resources.png" alt-text="Screenshot of the Remediate node and the grid of resources to remediate." border="false":::
297332

@@ -327,7 +362,7 @@ Start-AzPolicyRemediation -Name 'myRemedation' -PolicyAssignmentId '/subscriptio
327362
```
328363

329364
You may also choose to adjust remediation settings through these optional parameters:
330-
- `-FailureThreshold` - Used to specify whether the remediation task should fail if the percentage of failures exceeds the given threshold. Provided as a number between 0 to 100. By default, the failure threshold is 100%.
365+
- `-FailureThreshold` - Used to specify whether the remediation task should fail if the percentage of failures exceeds the given threshold. Provided as a number between 0 to 100. By default, the failure threshold is 100%.
331366
- `-ParallelDeploymentCount` - Determines how many non-compliant resources to remediate in a given remediation task. The default value is 500 (the previous limit). The maximum number is 50,000 resources.
332367
- `-ResourceCount` - Determines how many resources to remediate at the same time. The allowed values are 1 to 30 resources at a time. The default value is 10.
333368

0 commit comments

Comments
 (0)