Skip to content

Commit 550b2de

Browse files
committed
powershell for initiatives3
1 parent 492a063 commit 550b2de

File tree

1 file changed

+53
-37
lines changed

1 file changed

+53
-37
lines changed

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

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -197,43 +197,59 @@ TBD
197197

198198

199199
1. Set up your environemnt variables
200-
```azurepowershell
201-
# Set up your environment variables.
202-
$subscriptionId = <your subscription ID>;
203-
$rg = Get-AzResourceGroup -Name <your resource group name>;
204-
Select-AzSubscription $subscriptionId;
205-
$logAnlayticsWorskspaceId=</subscriptions/$subscriptionId/resourcegroups/$rg.ResourceGroupName/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>>;
206-
207-
# Get the initiative defintion. In this example we'll use Initiative *Enable audit category group resource logging for supported resources to Log Analytics*, ResourceID "/providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5"
208-
$definition = Get-AzPolicySetDefinition |Where-Object ResourceID -eq /providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5;
209-
210-
#Set an assignment name and configure parameters. For this initiative, the parameters include the Log Analytics workspace id.
211-
$assignmentName=<your assignment name>;
212-
$params = @{"logAnalytics"="/subscriptions/$subscriptionId/resourcegroups/$($rg.ResourceGroupName)/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>"}
213-
# Assign the initiative using the parameters
214-
$policyAssignment=New-AzPolicyAssignment -Name $assignmentName -Scope $rg.ResourceId -PolicySetDefinition $definition -PolicyparameterObject $params -IdentityType 'SystemAssigned' -Location eastus;
215-
216-
# Assign the `Contributor` role to the system assigned Managed Identity. For other initiatives, check which roles are required.
217-
New-AzRoleAssignment -Scope $rg.ResourceId -ObjectId $policyAssignment.Identity.PrincipalId -RoleDefinitionName Contributor;
218-
219-
#Scan for policy compliance. The `Start-AzPolicyComplianceScan` command takes a few minutes to return
220-
Start-AzPolicyComplianceScan -ResourceGroupName $rg.ResourceGroupName;
221-
#$policyAssignment=Get-AzPolicyAssignment -Name $assignmentName -Scope "/subscriptions/$subscriptionId/resourcegroups/$($rg.ResourceGroupName)";
222-
223-
#Get a list of resources to remediate and the required parameters by calling `Get-AzPolicyState`
224-
$assignmentState=Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName;
225-
$policyAssignmentId=$assignmentState.PolicyAssignmentId[0];
226-
$policyDefinitionReferenceIds=$assignmentState.PolicyDefinitionReferenceId;
227-
228-
#For each resource type with non-compliant resources, start a remediation task.
229-
$policyDefinitionReferenceIds | ForEach-Object {
230-
$referenceId = $_
231-
Start-AzPolicyRemediation -ResourceGroupName $rg.ResourceGroupName -PolicyAssignmentId $policyAssignmentId -PolicyDefinitionReferenceId $referenceId -Name "$($rg.ResourceGroupName) remediation $referenceId";
232-
}
233-
234-
#Check the compliance state when the remediation tasks have completed.
235-
Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName|select-object IsCompliant , ResourceID
236-
```
200+
```azurepowershell
201+
# Set up your environment variables.
202+
$subscriptionId = <your subscription ID>;
203+
$rg = Get-AzResourceGroup -Name <your resource group name>;
204+
Select-AzSubscription $subscriptionId;
205+
$logAnlayticsWorskspaceId=</subscriptions/$subscriptionId/resourcegroups/$rg.ResourceGroupName/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>>;
206+
```
207+
1. Get the initiative defintion. In this example we'll use Initiative *Enable audit category group resource logging for supported resources to `
208+
Log Analytics*, ResourceID "/providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5"
209+
```azurepowershell
210+
$definition = Get-AzPolicySetDefinition |Where-Object ResourceID -eq /providers/Microsoft.Authorization/policySetDefinitions/f5b29bc4-feca-4cc6-a58a-772dd5e290a5;
211+
```
212+
1. Set an assignment name and configure parameters. For this initiative, the parameters include the Log Analytics workspace id.
213+
```azurepowershell
214+
$assignmentName=<your assignment name>;
215+
$params = @{"logAnalytics"="/subscriptions/$subscriptionId/resourcegroups/$($rg.ResourceGroupName)/providers/microsoft.operationalinsights/workspaces/<your log analytics workspace>"}
216+
```
217+
1. Assign the initiative using the parameters
218+
```azurepowershell
219+
$policyAssignment=New-AzPolicyAssignment -Name $assignmentName -Scope $rg.ResourceId -PolicySetDefinition $definition -PolicyparameterObject $params -IdentityType 'SystemAssigned' -Location eastus;
220+
```
221+
1. Assign the `Contributor` role to the system assigned Managed Identity. For other initiatives, check which roles are required.
222+
```azurepowershell
223+
New-AzRoleAssignment -Scope $rg.ResourceId -ObjectId $policyAssignment.Identity.PrincipalId -RoleDefinitionName Contributor;
224+
```
225+
1. Scan for policy compliance. The `Start-AzPolicyComplianceScan` command takes a few minutes to return
226+
```azurepowershell
227+
Start-AzPolicyComplianceScan -ResourceGroupName $rg.ResourceGroupName;
228+
```
229+
230+
231+
1. Get a list of resources to remediate and the required parameters by calling `Get-AzPolicyState`
232+
```azurepowershell
233+
$assignmentState=Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName;
234+
$policyAssignmentId=$assignmentState.PolicyAssignmentId[0];
235+
$policyDefinitionReferenceIds=$assignmentState.PolicyDefinitionReferenceId;
236+
```
237+
1. For each resource type with non-compliant resources, start a remediation task.
238+
```azurepowershell
239+
$policyDefinitionReferenceIds | ForEach-Object {
240+
$referenceId = $_
241+
Start-AzPolicyRemediation -ResourceGroupName $rg.ResourceGroupName -PolicyAssignmentId $policyAssignmentId -PolicyDefinitionReferenceId $referenceId -Name "$($rg.ResourceGroupName) remediation $referenceId";
242+
}
243+
```
244+
1. Check the compliance state when the remediation tasks have completed.
245+
```azurepowershell
246+
Get-AzPolicyState -PolicyAssignmentName $assignmentName -ResourceGroupName $rg.ResourceGroupName|select-object IsCompliant , ResourceID
247+
```
248+
249+
You can get your policy assignment details using the following command:
250+
```azurepowershell
251+
$policyAssignment=Get-AzPolicyAssignment -Name $assignmentName -Scope "/subscriptions/$subscriptionId/resourcegroups/$($rg.ResourceGroupName)";
252+
```
237253

238254

239255
## Remediation tasks

0 commit comments

Comments
 (0)