Unable to assign policy from Azure Powershell

jitesh 1 Reputation point
2021-09-09T04:00:17.367+00:00

Hi ,

I am trying to Assign policy using the Azure Powershell to the resource group

First Creating RG-----------------------
$rg=New-AzResourceGroup -Name "My_New_resource_Group" -Location "East Us"

Second Assigning Policy definition------------------
$Document=Get-AzPolicyDefinition | Where-Object{$_.Properties.DisplayName -eq "Audit Missing tagss on Resource Groups"}

Third Assigning policy to the RG-------------------------
New-AzPolicyAssignment -Name "CheckPolicy" -DisplayName "Check Policy" -Scope $rg.ResourceId -PolicyDefinition $Document

but I get this prompt asking for the allowed locations . How to resolve this

130556-image.png

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
836 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,421 Reputation points
    2021-09-09T13:50:27.993+00:00

    Hi @jitesh ,

    You should be able to resolve this issue by following the example provided in this document i.e.,

    $ResourceGroup = Get-AzResourceGroup -Name 'ResourceGroup11'  
    $Policy = Get-AzPolicyDefinition -BuiltIn | Where-Object {$_.Properties.DisplayName -eq 'Allowed locations'}  
    $Locations = Get-AzLocation | where displayname -like '*east*'  
    $AllowedLocations = @{'listOfAllowedLocations'=($Locations.location)}  
    New-AzPolicyAssignment -Name 'RestrictLocationPolicyAssignment' -PolicyDefinition $Policy -Scope $ResourceGroup.ResourceId -PolicyParameterObject $AllowedLocations  
    

    Let me know if you have any further queries w.r.t it.