Bylaw

Gallery › Compute › Azure Community Policy

Audit if extension does not exist

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

This policy audits if a required extension doesn't exist.

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

Azure Policy JSONazurepolicy.json · 81 lines
{
  "name": "3752bb81-7d05-4e98-852f-49fe27b0b3d7",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Audit if extension does not exist",
    "description": "This policy audits if a required extension doesn't exist.",
    "metadata": {
      "version": "1.0.0",
      "category": "Compute"
    },
    "mode": "All",
    "parameters": {
      "publisher": {
        "type": "String",
        "metadata": {
          "description": "The publisher of the extension",
          "displayName": "Extension Publisher"
        }
      },
      "type": {
        "type": "String",
        "metadata": {
          "description": "The type of the extension",
          "displayName": "Extension Type"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "field": "Microsoft.Compute/imagePublisher",
            "in": [
              "MicrosoftWindowsServer"
            ]
          },
          {
            "field": "Microsoft.Compute/imageOffer",
            "in": [
              "WindowsServer"
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "[parameters('publisher')]"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "[parameters('type')]"
              }
            ]
          }
        }
      }
    }
  }
}
Bylaw3752bb81-7d05-4e98-852f-49fe27b0b3d7.bylaw.ps1 · 46 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '3752bb81-7d05-4e98-852f-49fe27b0b3d7' {

    Properties @{
        DisplayName = 'Audit if extension does not exist'
        Description = 'This policy audits if a required extension doesn''t exist.'
        Version     = '1.0.0'
        Category    = 'Compute'
        Mode        = 'All'
    }

    Parameter Publisher @{
        Type        = 'String'
        DisplayName = 'Extension Publisher'
        Description = 'The publisher of the extension'
    }
    Parameter Type @{
        Type        = 'String'
        DisplayName = 'Extension Type'
        Description = 'The type of the extension'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field 'Microsoft.Compute/imagePublisher' | Must -BeIn @('MicrosoftWindowsServer')
        }

        Require 'imageOffer is not one of 1 value' {
            Field 'Microsoft.Compute/imageOffer' | Must -Not -BeIn @('WindowsServer') -IfPresent
        }
    }

    AuditIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Effect (Param Effect) {
        Require 'publisher is the Publisher parameter and type is the Type parameter' {
            Field publisher | Must -Be (Param Publisher)
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be (Param Type)
        }
    }
}

Source: policyDefinitions/Compute/audit-if-extension-does-not-exist/azurepolicy.json in Azure/Community-Policy, under the MIT licence.