Bylaw

Gallery › Guest Configuration › Azure built-in policies

Deploy the Windows Guest Configuration extension to enable Guest Configuration assignments on Windows VMs

Converted Policy DeployIfNotExists Mode Indexed Version 1.3.0

This policy deploys the Windows Guest Configuration extension to Windows virtual machines hosted in Azure that are supported by Guest Configuration. The Windows Guest Configuration extension is a prerequisite for all Windows Guest Configuration assignments and must be deployed to machines before using any Windows Guest Configuration policy definition. For more information on Guest Configuration, visit https://aka.ms/gcpol.

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

Azure Policy JSONDeployExtensionWindows_Prerequisite.json · 249 lines
{
  "properties": {
    "displayName": "Deploy the Windows Guest Configuration extension to enable Guest Configuration assignments on Windows VMs",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "This policy deploys the Windows Guest Configuration extension to Windows virtual machines hosted in Azure that are supported by Guest Configuration. The Windows Guest Configuration extension is a prerequisite for all Windows Guest Configuration assignments and must be deployed to machines before using any Windows Guest Configuration policy definition. For more information on Guest Configuration, visit https://aka.ms/gcpol.",
    "metadata": {
      "category": "Guest Configuration",
      "version": "1.3.0"
    },
    "version": "1.3.0",
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Compute/imagePublisher",
                "in": [
                  "esri",
                  "incredibuild",
                  "MicrosoftDynamicsAX",
                  "MicrosoftSharepoint",
                  "MicrosoftVisualStudio",
                  "MicrosoftWindowsDesktop",
                  "MicrosoftWindowsServerHPCPack"
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageSKU",
                    "notLike": "2008*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftSQLServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "notLike": "SQL2008*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "microsoft-dsvm"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "like": "dsvm-win*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "microsoft-ads"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "in": [
                      "standard-data-science-vm",
                      "windows-data-science-vm"
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "batch"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "rendering-windows2016"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "center-for-internet-security-inc"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "like": "cis-windows-server-201*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "pivotal"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "like": "bosh-windows-server*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "cloud-infrastructure-services"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "like": "ad*"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Compute/virtualMachines/osProfile.windowsConfiguration",
                        "exists": "true"
                      },
                      {
                        "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
                        "like": "Windows*"
                      }
                    ]
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Compute/imageSKU",
                        "exists": "false"
                      },
                      {
                        "allOf": [
                          {
                            "field": "Microsoft.Compute/imageSKU",
                            "notLike": "2008*"
                          },
                          {
                            "field": "Microsoft.Compute/imageOffer",
                            "notLike": "SQL2008*"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "deployIfNotExists",
        "details": {
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "evaluationDelay": "AfterProvisioning",
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "name": "AzurePolicyforWindows",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "Microsoft.GuestConfiguration"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "ConfigurationforWindows"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2019-07-01",
                    "name": "[concat(parameters('vmName'), '/AzurePolicyforWindows')]",
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "location": "[parameters('location')]",
                    "properties": {
                      "publisher": "Microsoft.GuestConfiguration",
                      "type": "ConfigurationforWindows",
                      "typeHandlerVersion": "1.1",
                      "autoUpgradeMinorVersion": true,
                      "enableAutomaticUpgrade": true,
                      "settings": {},
                      "protectedSettings": {}
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.3.0",
      "1.2.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/385f5831-96d4-41db-9a3c-cd3af78aaae6",
  "name": "385f5831-96d4-41db-9a3c-cd3af78aaae6"
}
Bylaw385f5831-96d4-41db-9a3c-cd3af78aaae6.bylaw.ps1 · 103 lines
# Converted from DeployExtensionWindows_Prerequisite.json by ConvertTo-Bylaw.
Policy '385f5831-96d4-41db-9a3c-cd3af78aaae6' {

    Properties @{
        DisplayName   = 'Deploy the Windows Guest Configuration extension to enable Guest Configuration assignments on Windows VMs'
        Description   = 'This policy deploys the Windows Guest Configuration extension to Windows virtual machines hosted in Azure that are supported by Guest Configuration. The Windows Guest Configuration extension is a prerequisite for all Windows Guest Configuration assignments and must be deployed to machines before using any Windows Guest Configuration policy definition. For more information on Guest Configuration, visit https://aka.ms/gcpol.'
        Category      = 'Guest Configuration'
        Version       = '1.3.0'
        PolicyVersion = '1.3.0'
        Mode          = 'Indexed'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'imagePublisher is not one of 7 values' {
            Field 'Microsoft.Compute/imagePublisher' | Must -Not -BeIn @('esri', 'incredibuild', 'MicrosoftDynamicsAX', 'MicrosoftSharepoint', 'MicrosoftVisualStudio', 'MicrosoftWindowsDesktop', 'MicrosoftWindowsServerHPCPack') -IfPresent
        }

        Require 'imagePublisher and imageSKU are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftWindowsServer' -IfPresent
                Field 'Microsoft.Compute/imageSKU' | Must -BeLike '2008*'
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftSQLServer' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -BeLike 'SQL2008*'
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'microsoft-dsvm' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'dsvm-win*' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'microsoft-ads' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeIn @('standard-data-science-vm', 'windows-data-science-vm') -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'batch' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'rendering-windows2016' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'center-for-internet-security-inc' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'cis-windows-server-201*' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'pivotal' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'bosh-windows-server*' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'cloud-infrastructure-services' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'ad*' -IfPresent
            }
        }

        Require 'osProfile.windowsConfiguration, storageProfile.osDisk.osType, imageSKU and 1 more are as required' {
            AnyOf {
                AllOf {
                    Field osProfile.windowsConfiguration | Must -Not -Exist
                    Field storageProfile.osDisk.osType | Must -Not -BeLike 'Windows*' -IfPresent
                }
                AllOf {
                    Field 'Microsoft.Compute/imageSKU' | Must -Exist
                    AnyOf {
                        Field 'Microsoft.Compute/imageSKU' | Must -BeLike '2008*'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike 'SQL2008*'
                    }
                }
            }
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Contributor' -Name 'AzurePolicyforWindows' -EvaluationDelay AfterProvisioning {
        Require 'publisher, type and provisioningState are as required' {
            Field publisher | Must -Be 'Microsoft.GuestConfiguration'
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'ConfigurationforWindows'
            Field provisioningState | Must -Be 'Succeeded'
        }
        Deployment -TemplateFile './templates/385f5831-96d4-41db-9a3c-cd3af78aaae6.json' -Parameters ([ordered]@{
                vmName   = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Guest Configuration/DeployExtensionWindows_Prerequisite.json in Azure/azure-policy, under the MIT licence.