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