Bylaw

Gallery › Compute › Azure Community Policy

Allow custom VM image from a Resource Group

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

This policy allows only usage of images from a resource group

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

Azure Policy JSONazurepolicy.json · 54 lines
{
  "name": "841c8cb0-cce0-4716-a026-5254dd6468c6",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Allow custom VM image from a Resource Group",
    "description": "This policy allows only usage of images from a resource group",
    "metadata": {
      "version": "1.0.0",
      "category": "Compute"
    },
    "mode": "All",
    "parameters": {
      "resourceGroupName": {
        "type": "String",
        "metadata": {
          "displayName": "Resource Group Name",
          "strongType": "ExistingResourceGroups"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "not": {
              "field": "Microsoft.Compute/imageId",
              "contains": "[concat('resourceGroups/',parameters('resourceGroupName'))]"
            }
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw841c8cb0-cce0-4716-a026-5254dd6468c6.bylaw.ps1 · 32 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '841c8cb0-cce0-4716-a026-5254dd6468c6' {

    Properties @{
        DisplayName = 'Allow custom VM image from a Resource Group'
        Description = 'This policy allows only usage of images from a resource group'
        Version     = '1.0.0'
        Category    = 'Compute'
        Mode        = 'All'
    }

    Parameter ResourceGroupName @{
        Type        = 'String'
        DisplayName = 'Resource Group Name'
        Metadata    = [ordered]@{ strongType = 'ExistingResourceGroups' }
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'imageId contains (Arm ''[concat(''''resourceGroups/'''',parameters(''''resourceGroupName''''))]'')' {
            Field 'Microsoft.Compute/imageId' | Must -ContainText (Arm '[concat(''resourceGroups/'',parameters(''resourceGroupName''))]')
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/Compute/allow-custom-vm-image-from-a-resource-group/azurepolicy.json in Azure/Community-Policy, under the MIT licence.