Skip to content

Commit 69d8f7c

Browse files
committed
added Powershell for policies
1 parent ebee0df commit 69d8f7c

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,51 @@ az policy remediation create -g rg-001 -n remediation-001 --policy-assignment p
9898
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)
9999
### [PowerShell](#tab/Powershell)
100100

101-
Get from dev
101+
To apply a policy using the PowerShell, use the following commands:
102+
103+
1. Set up your environment.
104+
Select your subscription and set your resource group
105+
```azurepowershell
106+
Select-AzSubscription <subscriptionID>
107+
$rg = Get-AzResourceGroup -Name <resource groups name>
108+
```
109+
110+
1. Get the policy defintiion and configure the parameters for the policy. In the example below we assign the policy to send keyVault logs to a Log Analytics workspace
111+
```azurepowershell
112+
$definition = Get-AzPolicyDefinition |Where-Object Name -eq 6b359d8f-f88d-4052-aa7c-32015963ecc1
113+
$params = @{"logAnalytics"="/subscriptions/<subscriptionID/resourcegroups/<resourcgroup>/providers/microsoft.operationalinsights/workspaces/<log anlaytics workspace name>"}
114+
```
115+
116+
1. Assign the policy
117+
```azurepowershell
118+
$policyAssignment=New-AzPolicyAssignment -Name <assignment name> -DisplayName "assignment display name" -Scope $rg.ResourceId -PolicyDefinition $definition -PolicyparameterObject $params -IdentityType 'SystemAssigned' -Location <location>
119+
120+
```
121+
122+
1. Assign the required role or roles to the system assigned Managed Identity
123+
```azurepowershell
124+
$principalID=$policyAssignment.Identity.PrincipalId
125+
$roleDefinitionIds=$definition.Properties.policyRule.then.details.roleDefinitionIds
126+
$roleDefinitionIds | ForEach-Object {
127+
$roleDefId = $_.Split("/") | Select-Object -Last 1
128+
New-AzRoleAssignment -Scope $rg.ResourceId -ObjectId $policyAssignment.Identity.PrincipalId -RoleDefinitionId $roleDefId
129+
}
130+
```
131+
132+
Start-AzPolicyComplianceScan -ResourceGroupName $rg.ResourceGroupName
133+
1. Scan for compliance, then create a remediation task to force compliance for existing resources.
134+
```azurepowershell
135+
Start-AzPolicyComplianceScan -ResourceGroupName $rg.ResourceGroupName
136+
Start-AzPolicyRemediation -Name $policyAssignment.Name -PolicyAssignmentId $policyAssignment.PolicyAssignmentId -ResourceGroupName $rg.ResourceGroupName
137+
```
138+
139+
1. Check compliance
140+
```azurepowershell
141+
Get-AzPolicyState -PolicyAssignmentName $policyAssignment.Name -ResourceGroupName $policyAssignment.ResourceGroupName|select-object IsCompliant , ResourceID
142+
```
143+
144+
145+
$policyAssignment=Get-AzPolicyAssignment -Name 'VaultsLAWorkspace-04' -Scope '/subscriptions/d0567c0b-5849-4a5d-a2eb-5267eae1bbc7/resourcegroups/ed-ps-policy'
102146

103147
---
104148
## Assign initiatives
@@ -110,12 +154,6 @@ Initiatives are collections of policies. There are three initiatives for Azure M
110154

111155
In this example, we assign an initiative for sending audit logs to a Log Analytics workspace.
112156

113-
114-
115-
116-
117-
118-
119157
1. From the policy **Definitions** page, select your scope.
120158

121159
1. Select *Initiative* in the **Definition type** dropdown.

0 commit comments

Comments
 (0)