Skip to content

Commit f9850d2

Browse files
committed
final
1 parent 737d152 commit f9850d2

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

articles/azure-monitor/essentials/diagnostics-settings-policies-deployifnotexists.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ Policies and policy initiatives provide a simple method to enable logging at-sca
1515
Enable resource logs to track activities and events that take place on your resources and give you visibility and insights into any changes that occur.
1616
Assign policies to enable resource logs and to send them to destinations according to your needs. Send logs to Event Hubs for third-party SIEM systems, enabling continuous security operations. Send logs to storage accounts for longer term storage or the fulfillment of regulatory compliance.
1717

18-
A set of built-in policies and initiatives exists to direct resource logs to Log Analytics Workspaces, Event Hubs, and Storage Accounts.
19-
20-
The policies enable audit logging, sending logs belonging to the **audit** log category group to an Event Hub, Log Analytics workspace or Storage Account.
21-
22-
The policies' `effect` is `DeployIfNotExists`, which deploys the policy as a default if there aren't other settings defined.
18+
A set of built-in policies and initiatives exists to direct resource logs to Log Analytics Workspaces, Event Hubs, and Storage Accounts. The policies enable audit logging, sending logs belonging to the **audit** log category group to an Event Hub, Log Analytics workspace or Storage Account. The policies' `effect` is `DeployIfNotExists`, which deploys the policy as a default if there aren't other settings defined.
2319

2420

2521
## Deploy policies.
@@ -52,50 +48,54 @@ The policy visible in the resources' diagnostic setting after approximately 30 m
5248
### [CLI](#tab/cli)
5349
To apply a policy using the CLI, use the following commands:
5450

55-
1. Create a policy assignment using
56-
```azurecli
57-
58-
az policy assignment create --name <policy assignment name> --policy "6b359d8f-f88d-4052-aa7c-32015963ecc1" --scope <scope> --params "{\"logAnalytics\": {\"value\": \"<log analytics workspace resource ID"}}" --mi-system-assigned --location <location>
59-
```
60-
For example, to apply the policy to send audit logs to a log analytics workspace
51+
1. Create a policy assignment using [`az policy assignment create`](https://learn.microsoft.com/cli/azure/policy/assignment?view=azure-cli-latest#az-policy-assignment-create).
52+
```azurecli
53+
az policy assignment create --name <policy assignment name> --policy "6b359d8f-f88d-4052-aa7c-32015963ecc1" --scope <scope> --params "{\"logAnalytics\": {\"value\": \"<log analytics workspace resource ID"}}" --mi-system-assigned --location <location>
54+
```
55+
For example, to apply the policy to send audit logs to a log analytics workspace
6156

62-
```azurecli
63-
az policy assignment create --name "policy-assignment-1" --policy "6b359d8f-f88d-4052-aa7c-32015963ecc1" --scope /subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourceGroups/rg-001 --params "{\"logAnalytics\": {\"value\": \"/subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourcegroups/rg-001/providers/microsoft.operationalinsights/workspaces/workspace-001\"}}" --mi-system-assigned --location eastus
64-
```
57+
```azurecli
58+
az policy assignment create --name "policy-assignment-1" --policy "6b359d8f-f88d-4052-aa7c-32015963ecc1" --scope /subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourceGroups/rg-001 --params "{\"logAnalytics\": {\"value\": \"/subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourcegroups/rg-001/providers/microsoft.operationalinsights/workspaces/workspace-001\"}}" --mi-system-assigned --location eastus
59+
```
6560

66-
2. Assign the required role to the identity created for the policy assignment.
61+
1. Assign the required role to the identity created for the policy assignment.
6762
Find the role in the policy definition by searching for *roleDefinitionIds*
6863

69-
```json
64+
```json
7065
...},
7166
"roleDefinitionIds": [
7267
"/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
7368
],
7469
"deployment": {
7570
"properties": {...
76-
```
77-
78-
```azurecli
79-
az policy assignment identity assign --system-assigned -g <resource group name> --role <role name or ID> --identity-scope </scope> -n <policy assignment name>
80-
```
81-
For example:
71+
```
72+
Assign the required role using [`az policy assignment identity assign`](https://learn.microsoft.com/cli/azure/policy/assignment/identity?view=azure-cli-latest):
73+
```azurecli
74+
az policy assignment identity assign --system-assigned --resource-group <resource group name> --role <role name or ID> --identity-scope </scope> --name <policy assignment name>
75+
```
76+
For example:
77+
```azurecli
78+
az policy assignment identity assign --system-assigned --resource-group rg-001 --role 92aaf0da-9dab-42b6-94a3-d43ce8d16293 --identity-scope /subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourceGroups/rg001 --name policy-assignment-1
79+
```
80+
1. Trigger a scan to find existing resources using [`az policy state trigger-scan`](https://learn.microsoft.com/cli/azure/policy/state?view=azure-cli-latest#az-policy-state-trigger-scan).
8281

83-
```azurecli
84-
az policy assignment identity assign --system-assigned -g rg-001 --role 92aaf0da-9dab-42b6-94a3-d43ce8d16293 --identity-scope /subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourceGroups/rg001 -n policy-assignment-1
85-
```
82+
```azurecli
83+
az policy state trigger-scan --resource-group rg-001
84+
```
8685

87-
3. Create a remediation task to apply the policy to existing resources.
86+
1. Create a remediation task to apply the policy to existing resources using [`az policy remediation create`](https://learn.microsoft.com/cli/azure/policy/remediation?view=azure-cli-latest#az-policy-remediation-create).
8887

89-
```azurecli
90-
az policy remediation create -g <resource group name> --policy-assignment <policy assignment name> --name <remediation name>
91-
```
88+
```azurecli
89+
az policy remediation create -g <resource group name> --policy-assignment <policy assignment name> --name <remediation name>
90+
```
9291

93-
For example,
94-
```azurecli
95-
az policy remediation create -g rg-001 -n remediation-001 --policy-assignment policy-assignment-1
96-
```
92+
For example,
93+
```azurecli
94+
az policy remediation create -g rg-001 -n remediation-001 --policy-assignment policy-assignment-1
95+
```
9796

9897
For more information on policy assignment using CLI, see [Azure CLI reference - az policy assignment](https://learn.microsoft.com/cli/azure/policy/assignment?view=azure-cli-latest#az-policy-assignment-create)
98+
9999
### [PowerShell](#tab/Powershell)
100100

101101
To apply a policy using the PowerShell, use the following commands:
@@ -222,20 +222,24 @@ Change the default name in the **Parameters** tab of the **Assign initiative** o
222222
Select-AzSubscription $subscriptionId;
223223
$logAnlayticsWorskspaceId=</subscriptions/$subscriptionId/resourcegroups/$rg.ResourceGroupName/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>;
224224
```
225+
225226
1. Get the initiative definition. In this example, we'll use Initiative *Enable audit category group resource logging for supported resources to `
226227
Log Analytics*, ResourceID "/providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5"
227228
```azurepowershell
228229
$definition = Get-AzPolicySetDefinition |Where-Object ResourceID -eq /providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5;
229230
```
231+
230232
1. Set an assignment name and configure parameters. For this initiative, the parameters include the Log Analytics workspace ID.
231233
```azurepowershell
232234
$assignmentName=<your assignment name>;
233235
$params = @{"logAnalytics"="/subscriptions/$subscriptionId/resourcegroups/$($rg.ResourceGroupName)/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>"}
234236
```
237+
235238
1. Assign the initiative using the parameters
236239
```azurepowershell
237240
$policyAssignment=New-AzPolicyAssignment -Name $assignmentName -Scope $rg.ResourceId -PolicySetDefinition $definition -PolicyparameterObject $params -IdentityType 'SystemAssigned' -Location eastus;
238241
```
242+
239243
1. Assign the `Contributor` role to the system assigned Managed Identity. For other initiatives, check which roles are required.
240244
```azurepowershell
241245
New-AzRoleAssignment -Scope $rg.ResourceId -ObjectId $policyAssignment.Identity.PrincipalId -RoleDefinitionName Contributor;
@@ -244,19 +248,22 @@ Log Analytics*, ResourceID "/providers/Microsoft.Authorization/policySetDefinit
244248
```azurepowershell
245249
Start-AzPolicyComplianceScan -ResourceGroupName $rg.ResourceGroupName;
246250
```
251+
247252
1. Get a list of resources to remediate and the required parameters by calling `Get-AzPolicyState`
248253
```azurepowershell
249254
$assignmentState=Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName;
250255
$policyAssignmentId=$assignmentState.PolicyAssignmentId[0];
251256
$policyDefinitionReferenceIds=$assignmentState.PolicyDefinitionReferenceId;
252257
```
258+
253259
1. For each resource type with non-compliant resources, start a remediation task.
254260
```azurepowershell
255261
$policyDefinitionReferenceIds | ForEach-Object {
256262
$referenceId = $_
257263
Start-AzPolicyRemediation -ResourceGroupName $rg.ResourceGroupName -PolicyAssignmentId $policyAssignmentId -PolicyDefinitionReferenceId $referenceId -Name "$($rg.ResourceGroupName) remediation $referenceId";
258264
}
259265
```
266+
260267
1. Check the compliance state when the remediation tasks have completed.
261268
```azurepowershell
262269
Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName|select-object IsCompliant , ResourceID
@@ -271,9 +278,10 @@ You can get your policy assignment details using the following command:
271278

272279

273280
1. Sign in to your Azure account using the `az login` command.
274-
1. Select the subscription where you want to apply the policy initiative using the `az account` set command.
281+
1.
282+
1. Select the subscription where you want to apply the policy initiative using the `az account set` command.
275283

276-
1. Assign the initiative using [az policy assignment create](https://learn.microsoft.com/cli/azure/policy/assignment?view=azure-cli-latest#az-policy-assignment-create).
284+
1. Assign the initiative using [`az policy assignment create`](https://learn.microsoft.com/cli/azure/policy/assignment?view=azure-cli-latest#az-policy-assignment-create).
277285

278286
```azurecli
279287
az policy assignment create --name <assignment name> --resource-group <resource group name> --policy-set-definition <initiative name> --params <parameters object> --mi-system-assigned --location <location>
@@ -295,7 +303,7 @@ You can get your policy assignment details using the following command:
295303
"deployment": {
296304
"properties": {...
297305
```
298-
Assign the required role using [az policy assignment identity assign](https://learn.microsoft.com/cli/azure/policy/assignment/identity?view=azure-cli-latest):
306+
Assign the required role using [`az policy assignment identity assign`](https://learn.microsoft.com/cli/azure/policy/assignment/identity?view=azure-cli-latest):
299307
```azurecli
300308
az policy assignment identity assign --system-assigned --resource-group <resource group name> --role <role name or ID> --identity-scope <scope> --name <policy assignment name>
301309
```
@@ -304,13 +312,14 @@ You can get your policy assignment details using the following command:
304312
```azurecli
305313
az policy assignment identity assign --system-assigned --resource-group "cli-example-01" --role 92aaf0da-9dab-42b6-94a3-d43ce8d16293 --identity-scope "/subscriptions/12345678-aaaa-bbbb-cccc-1234567890ab/resourcegroups/cli-example-01" --name assign-cli-example-01
306314
```
315+
307316
1. Create remediation tasks for the policies in the initiative.
308317
309318
Remediation tasks are created per-policy. Each task is for a specific `definition-reference-id`, specified in the initiative as `policyDefinitionReferenceId`. To find the `definition-reference-id` parameter, use the following command:
310319
```azurecli
311320
az policy set-definition show --name f5b29bc4-feca-4cc6-a58a-772dd5e290a5 |grep policyDefinitionReferenceId
312321
```
313-
Remediate the resources using [az policy remediation create](https://learn.microsoft.com/cli/azure/policy/remediation?view=azure-cli-latest#az-policy-remediati
322+
Remediate the resources using [`az policy remediation create`]https://learn.microsoft.com/cli/azure/policy/remediation?view=azure-cli-latest#az-policy-remediation-create)
314323
315324
```azurecli
316325
az policy remediation create --resource-group <resource group name> --policy-assignment <assignment name> --name <remediation task name> --definition-reference-id "policy specific reference ID" --resource-discovery-mode ReEvaluateCompliance

0 commit comments

Comments
 (0)