Bylaw

Gallery › Compute › Azure Community Policy

Deploy Azure Hybrid Benefit for Windows.

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy ensures virtual machines are configured for Azure Hybrid Benefit for Windows Client and Server - https://docs.microsoft.com/en-us/azure/virtual-machines/windows/hybrid-use-benefit-licensing#ways-to-use-azure-hybrid-benefit-for-windows-server.

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

Azure Policy JSONazurepolicy.json · 118 lines
{
  "name": "0ad1b72f-ed9c-40aa-84cf-6e4f2820bc59",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Azure Hybrid Benefit for Windows.",
    "description": "This policy ensures virtual machines are configured for Azure Hybrid Benefit for Windows Client and Server - https://docs.microsoft.com/en-us/azure/virtual-machines/windows/hybrid-use-benefit-licensing#ways-to-use-azure-hybrid-benefit-for-windows-server.",
    "metadata": {
      "category": "Compute",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "field": "Microsoft.Compute/imagePublisher",
            "in": [
              "MicrosoftWindowsServer",
              "MicrosoftWindowsDesktop"
            ]
          },
          {
            "field": "Microsoft.Compute/imageOffer",
            "in": [
              "WindowsServer",
              "Windows-10"
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/licenseType",
                "equals": "[if(equals(field('Microsoft.Compute/imagePublisher'), 'MicrosoftWindowsDesktop'), 'Windows_Client', 'Windows_Server')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmName": {
                    "type": "String"
                  },
                  "licenceType": {
                    "defaultValue": "None",
                    "type": "String",
                    "allowedValues": [
                      "None",
                      "Windows_Client",
                      "Windows_Server"
                    ]
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Compute/virtualMachines",
                    "apiVersion": "2021-03-01",
                    "name": "[parameters('vmName')]",
                    "location": "[resourceGroup().location]",
                    "properties": {
                      "licenseType": "[parameters('licenceType')]"
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Changed LicenseType for Windows VM', ': ', parameters('vmName'), '- ', parameters('licenceType'))]"
                  }
                }
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "licenceType": {
                  "value": "[if(equals(field('Microsoft.Compute/imagePublisher'), 'MicrosoftWindowsDesktop'), 'Windows_Client', 'Windows_Server')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw0ad1b72f-ed9c-40aa-84cf-6e4f2820bc59.bylaw.ps1 · 39 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '0ad1b72f-ed9c-40aa-84cf-6e4f2820bc59' {

    Properties @{
        DisplayName = 'Deploy Azure Hybrid Benefit for Windows.'
        Description = 'This policy ensures virtual machines are configured for Azure Hybrid Benefit for Windows Client and Server - https://docs.microsoft.com/en-us/azure/virtual-machines/windows/hybrid-use-benefit-licensing#ways-to-use-azure-hybrid-benefit-for-windows-server.'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

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

        Require 'imageOffer is not one of 2 values' {
            Field 'Microsoft.Compute/imageOffer' | Must -Not -BeIn @('WindowsServer', 'Windows-10') -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines' -Role 'Virtual Machine Contributor' -Effect (Param Effect) {
        Require 'licenseType and imagePublisher are as required' {
            Field licenseType | Must -Be (Arm '[if(equals(field(''Microsoft.Compute/imagePublisher''), ''MicrosoftWindowsDesktop''), ''Windows_Client'', ''Windows_Server'')]')
        }
        Deployment -TemplateFile './templates/0ad1b72f-ed9c-40aa-84cf-6e4f2820bc59.json' -Parameters ([ordered]@{
                vmName      = (Arm '[field(''name'')]')
                licenceType = (Arm '[if(equals(field(''Microsoft.Compute/imagePublisher''), ''MicrosoftWindowsDesktop''), ''Windows_Client'', ''Windows_Server'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Compute/deploy-azure-hybrid-benefit-for-windows/azurepolicy.json in Azure/Community-Policy, under the MIT licence.