Exclude Windows Server from VM Shutdown Azure Policy

Ben Woodman 106 Reputation points
2023-08-14T10:09:05.7666667+00:00

Hi,

I currently have a custom built Azure policy to mark any VM as non-compliant if the auto-shutdown feature is not enabled (There is no remediation task currently).

We do not want this policy applied to Windows server 2019 VMs, is there anyway to exclude resources in the policy via tags or operating systems at all?

So for example if tags i would assign a tag to all the windows servers and then the policy would exclude them if the tag has been placed against the resource. Or if better the OS, for example if a VM has the OS Windows Server 2019 then exclude from the policy.

Any help would be greatly appreciated.

Kind regards,

Ben

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

2 answers

Sort by: Most helpful
  1. Luke Murray 11,351 Reputation points MVP
    2023-08-21T20:14:40.25+00:00

    Hi, yes you can.

    You should be able to add a if not true to the police for the image Offer:

       "not": {
              "allOf": [
                {
                  "field": "Microsoft.Compute/virtualmachines/imageOffer",
                  "in": [
                    "2019*"
                  ]
                },
    
    0 comments No comments

  2. Ryan Hill 30,026 Reputation points Microsoft Employee
    2023-08-21T20:50:33.5766667+00:00

    Hi @Ben Woodman

    Another option is leveraging New-AzPolicyExemption cmdlet to exclude by operating system. An example of creating a policy using Azure PowerShell

    $exemption = New-AzPolicyExemption `
        -Name "Exclude Windows Server 2019 VMs from auto-shutdown policy" `
        -PolicyAssignmentId "/subscriptions/{subscriptionId}/providers/Microsoft.Management/managementGroups/{managementGroupId}/providers/Microsoft.Authorization/policyAssignments/{policyAssignmentId}" `
        -DisplayName "Exclude Windows Server 2019 VMs from auto-shutdown policy" `
        -Description "Excludes Windows Server 2019 VMs from the auto-shutdown policy" `
        -Metadata @{"category"="Auto-shutdown policy";"notes"="Excludes Windows Server 2019 VMs from the auto-shutdown policy"} `
        -TargetResourceId "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}" `
        -ExemptionCategory "Mitigated" `
        -ExpirationDate (Get-Date).AddDays(30) `
        -PolicyDefinitionReferenceId "/providers/Microsoft.Authorization/policyDefinitions/{policyDefinitionId}" `
        -Properties @{"osType"="Windows";"osVersion"="2019"} `
        -Reason "Excluding Windows Server 2019 VMs from the auto-shutdown policy" `
        -CreatedBy "John Doe" `
        -CreatedOn (Get-Date)
    

    Once created, you can use Set-AzPolicyAssignment cmdlet to assign the policy.

    $vms = Get-AzVM -ResourceGroupName "myResourceGroup" | ForEach-Object { $_.Id }
    $exemption = Get-AzPolicyExemption -Name "Exclude Windows Server 2019 VMs from auto-shutdown policy"
    $exemption.Parameters.ExcludeResourceIds.Value = $vms
    Set-AzPolicyAssignment -PolicyAssignment $exemption -Id $exemption.Id
    
    0 comments No comments

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.