Bylaw

Gallery › Compute › Azure Community Policy

Deploy hybrid use for Windows Server

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This Policy will enable HUB for Windows Server

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

Azure Policy JSONazurepolicy.json · 96 lines
{
  "name": "7a53edd3-ebf5-4698-a52a-8c031406a20e",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy hybrid use for Windows Server",
    "description": "This Policy will enable HUB for Windows Server",
    "metadata": {
      "category": "Compute",
      "version": "1.0.0"
    },
    "mode": "All",
    "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/virtualMachines/imageOffer",
            "equals": "windowsserver"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines",
          "name": "[field('name')]",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/licenseType",
                "notEquals": "Windows_Server"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "windowsServerName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[parameters('windowsServerName')]",
                    "type": "Microsoft.Compute/virtualMachines",
                    "apiVersion": "2019-07-01",
                    "location": "[parameters('location')]",
                    "properties": {
                      "licenseType": "Windows_Server"
                    }
                  }
                ]
              },
              "parameters": {
                "windowsServerName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw7a53edd3-ebf5-4698-a52a-8c031406a20e.bylaw.ps1 · 35 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '7a53edd3-ebf5-4698-a52a-8c031406a20e' {

    Properties @{
        DisplayName = 'Deploy hybrid use for Windows Server'
        Description = 'This Policy will enable HUB for Windows Server'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'All'
    }

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

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'imageOffer is not windowsserver' {
            Field imageOffer | Must -Not -Be 'windowsserver' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines' -Role 'Owner' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'licenseType is not Windows_Server' {
            Field licenseType | Must -Not -Be 'Windows_Server' -IfPresent
        }
        Deployment -TemplateFile './templates/7a53edd3-ebf5-4698-a52a-8c031406a20e.json' -Parameters ([ordered]@{
                windowsServerName = (Arm '[field(''name'')]')
                location          = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

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