Azure policy exemption creation using powershell

MrFlinstone 686 Reputation points
2024-01-15T17:25:59.07+00:00

Trying to create policy exemptions using code as opposed to manually creating them. According to the MS link I have a policy initiative which consists of multiple sub policies. The first issue is that I cannot seem to get the correct -Name passed to New-AzPolicyExemption This doesn't work.


    $mgmtGroup = 'xxx'
    $policySetName = 'xxx'
    Get-AzPolicySetDefinition -ManagementGroupName $mgmtGroup -Custom | Where {$_.Properties.DisplayName -eq $policySetName } | Select-Object Name

I can see the display name on the portal, but I do not know what the internal name is. I believe this is stored within the properties object when you run `Get-AzPolicySetDefinition` how can one display all the values within the property object ?

Going back to the main question

Running the below fails because it cannot find the `Name` parameter.

    ResourceGroup = Get-AzResourceGroup -Name 'ResourceGroup11'
    $intiative = Get-AzPolicySetDefinition | Where-Object { $_.Properties.DisplayName -eq 'Public' }
    New-AzPolicyAssignment -Name 'denyPolicies' -PolicySetDefinition $intiative -Scope $ResourceGroup.ResourceId






Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,014 questions
{count} vote

1 answer

Sort by: Most helpful
  1. SwathiDhanwada-MSFT 18,996 Reputation points Moderator
    2024-01-16T04:44:12.5933333+00:00

    MrFlinstone Thanks for reaching out. The process you are following is correct. However, I will share an example of how I retrieved the mentioned information in my environment for your reference.

    In my example, I am retrieving the Policy Initiative "Enable Azure Monitor for VMSS with Azure Monitoring Agent(AMA)".

    $mgmtGroup="Non Production"                                                                                                              $policySetName="Audit Public Network Access"
    $definition = Get-AzPolicySetDefinition -ManagementGroupName $mgmtGroup | Where {$_.Properties.DisplayName -eq $policySetName } 
    

    You can the internal name of the policy set definition using below command which is what you have been using.

    Get-AzPolicySetDefinition -ManagementGroupName $mgmtGroup | Where {$_.Properties.DisplayName -eq

    $policySetName } | Select-Object Name

    User's image

    To display all the values of property object within policy set definition, you can either follow the process shown in above screenshot or execute below command.

    ( Get-AzPolicySetDefinition -ManagementGroupName $mgmtGroup | Where {$_.Properties.DisplayName -eq $policySetName }).Properties

    Regarding second error where you are prompted with Name not found , kindly share further information on the error by adding -debug property to command as I wasn't able to reproduce the error you are facing even though I am using similar commands as you.

    $rg = Get-AzResourceGroup -Name "amkiyu"
    New-AzPolicyAssignment -Name "audit PNA" -Scope $rg.ResourceId -PolicySetDefinition $definition
    

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.