Bylaw

Gallery › General › Azure Community Policy

Name pattern with match condition.

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0

Enforce a naming pattern on resources with the match condition.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 45 lines
{
  "name": "4af18e9a-6294-41a4-b048-e6b421719f26",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Name pattern with match condition.",
    "description": "Enforce a naming pattern on resources with the match condition.",
    "metadata": {
      "version": "1.0.0",
      "category": "General"
    },
    "mode": "Indexed",
    "parameters": {
      "namePattern": {
        "type": "String",
        "metadata": {
          "description": "Pattern to use for names. Can include ? for letters and # for numbers."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "not": {
          "field": "name",
          "match": "[parameters('namePattern')]"
        }
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw4af18e9a-6294-41a4-b048-e6b421719f26.bylaw.ps1 · 31 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '4af18e9a-6294-41a4-b048-e6b421719f26' {

    Properties @{
        DisplayName = 'Name pattern with match condition.'
        Description = 'Enforce a naming pattern on resources with the match condition.'
        Version     = '1.0.0'
        Category    = 'General'
        Mode        = 'Indexed'
    }

    Parameter NamePattern @{
        Type        = 'String'
        Description = 'Pattern to use for names. Can include ? for letters and # for numbers.'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource * {

        Require 'name matches the NamePattern parameter' {
            Field name | Must -MatchCase (Param NamePattern)
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/General/name-pattern-with-match-condition/azurepolicy.json in Azure/Community-Policy, under the MIT licence.