Bylaw

Gallery › Monitoring › Azure built-in policies

Configure Windows virtual machine scale sets to run Azure Monitor Agent using system-assigned managed identity

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 3.7.0

Automate the deployment of Azure Monitor Agent extension on your Windows virtual machine scale sets for collecting telemetry data from the guest OS. This policy will install the extension if the OS and region are supported and system-assigned managed identity is enabled, and skip install otherwise. Learn more: https://aka.ms/AMAOverview.

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

Azure Policy JSONAzureMonitor_Agent_Windows_VMSS_DINE.json · 425 lines
{
  "properties": {
    "displayName": "Configure Windows virtual machine scale sets to run Azure Monitor Agent using system-assigned managed identity",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Automate the deployment of Azure Monitor Agent extension on your Windows virtual machine scale sets for collecting telemetry data from the guest OS. This policy will install the extension if the OS and region are supported and system-assigned managed identity is enabled, and skip install otherwise. Learn more: https://aka.ms/AMAOverview.",
    "metadata": {
      "version": "3.7.0",
      "category": "Monitoring"
    },
    "version": "3.7.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy."
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "scopeToSupportedImages": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Scope Policy to Azure Monitor Agent-Supported Operating Systems",
          "description": "If set to true, the policy will apply only to virtual machine scale sets with supported operating systems. Otherwise, the policy will apply to all virtual machine scale set resources in the assignment scope. For supported operating systems, see https://aka.ms/AMAOverview."
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": true
      },
      "listOfWindowsImageIdToInclude": {
        "type": "Array",
        "metadata": {
          "displayName": "Additional Virtual Machine Images",
          "description": "List of virtual machine images that have supported Windows OS to add to scope. Example values: '/subscriptions/<subscriptionId>/resourceGroups/YourResourceGroup/providers/Microsoft.Compute/images/ContosoStdImage'"
        },
        "defaultValue": []
      },
      "listOfApplicableLocations": {
        "type": "Array",
        "metadata": {
          "displayName": "Applicable Locations",
          "description": "List of locations where the policy should apply.",
          "strongType": "location"
        },
        "defaultValue": [
          "australiacentral",
          "australiacentral2",
          "australiaeast",
          "australiasoutheast",
          "brazilsouth",
          "brazilsoutheast",
          "canadacentral",
          "canadaeast",
          "centralindia",
          "centralus",
          "centraluseuap",
          "eastasia",
          "eastus",
          "eastus2",
          "eastus2euap",
          "francecentral",
          "francesouth",
          "germanynorth",
          "germanywestcentral",
          "israelcentral",
          "italynorth",
          "japaneast",
          "japanwest",
          "jioindiacentral",
          "jioindiawest",
          "koreacentral",
          "koreasouth",
          "malaysiasouth",
          "mexicocentral",
          "northcentralus",
          "northeurope",
          "norwayeast",
          "norwaywest",
          "polandcentral",
          "qatarcentral",
          "southafricanorth",
          "southafricawest",
          "southcentralus",
          "southeastasia",
          "southindia",
          "spaincentral",
          "swedencentral",
          "swedensouth",
          "switzerlandnorth",
          "switzerlandwest",
          "taiwannorth",
          "taiwannorthwest",
          "uaecentral",
          "uaenorth",
          "uksouth",
          "ukwest",
          "westcentralus",
          "westeurope",
          "westindia",
          "westus",
          "westus2",
          "westus3",
          "chilecentral",
          "newzealandnorth"
        ]
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachineScaleSets"
          },
          {
            "field": "location",
            "in": "[parameters('listOfApplicableLocations')]"
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "value": "[parameters('scopeToSupportedImages')]",
                    "equals": false
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachineScaleSets/virtualMachineProfile.storageProfile.osDisk.osType",
                    "like": "Windows*"
                  }
                ]
              },
              {
                "field": "Microsoft.Compute/imageId",
                "in": "[parameters('listOfWindowsImageIdToInclude')]"
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "WindowsServer"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2008-R2-SP1*"
                      },
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2012-*"
                      },
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2016-*"
                      },
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2019-*"
                      },
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2022-*"
                      },
                      {
                        "field": "Microsoft.Compute/imageSku",
                        "like": "2025-*"
                      }
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "WindowsServerSemiAnnual"
                  },
                  {
                    "field": "Microsoft.Compute/imageSKU",
                    "in": [
                      "Datacenter-Core-1709-smalldisk",
                      "Datacenter-Core-1709-with-Containers-smalldisk",
                      "Datacenter-Core-1803-with-Containers-smalldisk"
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsServerHPCPack"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "WindowsServerHPCPack"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftSQLServer"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2025"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2025-BYOL"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2022"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2022-BYOL"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2019"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2019-BYOL"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2016"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2016-BYOL"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2012R2"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "*-WS2012R2-BYOL"
                      }
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftRServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "MLServer-WS2016"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftVisualStudio"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "in": [
                      "VisualStudio",
                      "Windows"
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftDynamicsAX"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "Dynamics"
                  },
                  {
                    "field": "Microsoft.Compute/imageSKU",
                    "equals": "Pre-Req-AX7-Onebox-U8"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "microsoft-ads"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "windows-data-science-vm"
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsDesktop"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "like": "Windows-1*"
                  }
                ]
              }
            ]
          },
          {
            "field": "identity.type",
            "contains": "SystemAssigned"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/type",
                "equals": "AzureMonitorWindowsAgent"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/publisher",
                "equals": "Microsoft.Azure.Monitor"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "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"
                  }
                },
                "variables": {
                  "extensionName": "AzureMonitorWindowsAgent",
                  "extensionPublisher": "Microsoft.Azure.Monitor",
                  "extensionType": "AzureMonitorWindowsAgent",
                  "extensionTypeHandlerVersion": "1.1"
                },
                "resources": [
                  {
                    "name": "[concat(parameters('vmName'), '/', variables('extensionName'))]",
                    "type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
                    "location": "[parameters('location')]",
                    "apiVersion": "2019-07-01",
                    "properties": {
                      "publisher": "[variables('extensionPublisher')]",
                      "type": "[variables('extensionType')]",
                      "typeHandlerVersion": "[variables('extensionTypeHandlerVersion')]",
                      "autoUpgradeMinorVersion": true,
                      "enableAutomaticUpgrade": true
                    }
                  }
                ]
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "3.7.0",
      "3.6.0",
      "3.5.0",
      "3.4.0",
      "3.3.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/4efbd9d8-6bc6-45f6-9be2-7fe9dd5d89ff",
  "name": "4efbd9d8-6bc6-45f6-9be2-7fe9dd5d89ff"
}
Bylaw4efbd9d8-6bc6-45f6-9be2-7fe9dd5d89ff.bylaw.ps1 · 126 lines
# Converted from AzureMonitor_Agent_Windows_VMSS_DINE.json by ConvertTo-Bylaw.
Policy '4efbd9d8-6bc6-45f6-9be2-7fe9dd5d89ff' {

    Properties @{
        DisplayName   = 'Configure Windows virtual machine scale sets to run Azure Monitor Agent using system-assigned managed identity'
        Description   = 'Automate the deployment of Azure Monitor Agent extension on your Windows virtual machine scale sets for collecting telemetry data from the guest OS. This policy will install the extension if the OS and region are supported and system-assigned managed identity is enabled, and skip install otherwise. Learn more: https://aka.ms/AMAOverview.'
        Version       = '3.7.0'
        Category      = 'Monitoring'
        PolicyVersion = '3.7.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy.'
    }
    Parameter ScopeToSupportedImages @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $true
        DisplayName   = 'Scope Policy to Azure Monitor Agent-Supported Operating Systems'
        Description   = 'If set to true, the policy will apply only to virtual machine scale sets with supported operating systems. Otherwise, the policy will apply to all virtual machine scale set resources in the assignment scope. For supported operating systems, see https://aka.ms/AMAOverview.'
    }
    Parameter ListOfWindowsImageIdToInclude @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Additional Virtual Machine Images'
        Description = 'List of virtual machine images that have supported Windows OS to add to scope. Example values: ''/subscriptions/<subscriptionId>/resourceGroups/YourResourceGroup/providers/Microsoft.Compute/images/ContosoStdImage'''
    }
    Parameter ListOfApplicableLocations @{
        Type        = 'Array'
        Default     = @('australiacentral', 'australiacentral2', 'australiaeast', 'australiasoutheast', 'brazilsouth', 'brazilsoutheast', 'canadacentral', 'canadaeast', 'centralindia', 'centralus', 'centraluseuap', 'eastasia', 'eastus', 'eastus2', 'eastus2euap', 'francecentral', 'francesouth', 'germanynorth', 'germanywestcentral', 'israelcentral', 'italynorth', 'japaneast', 'japanwest', 'jioindiacentral', 'jioindiawest', 'koreacentral', 'koreasouth', 'malaysiasouth', 'mexicocentral', 'northcentralus', 'northeurope', 'norwayeast', 'norwaywest', 'polandcentral', 'qatarcentral', 'southafricanorth', 'southafricawest', 'southcentralus', 'southeastasia', 'southindia', 'spaincentral', 'swedencentral', 'swedensouth', 'switzerlandnorth', 'switzerlandwest', 'taiwannorth', 'taiwannorthwest', 'uaecentral', 'uaenorth', 'uksouth', 'ukwest', 'westcentralus', 'westeurope', 'westindia', 'westus', 'westus2', 'westus3', 'chilecentral', 'newzealandnorth')
        DisplayName = 'Applicable Locations'
        Description = 'List of locations where the policy should apply.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }

    Resource 'Microsoft.Compute/virtualMachineScaleSets' {

        When {
            Field location | Must -BeIn (Param ListOfApplicableLocations)
            AnyOf {
                AllOf {
                    Param ScopeToSupportedImages | Must -Be $false
                    Field virtualMachineProfile.storageProfile.osDisk.osType | Must -BeLike 'Windows*'
                }
                Field 'Microsoft.Compute/imageId' | Must -BeIn (Param ListOfWindowsImageIdToInclude)
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftWindowsServer'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'WindowsServer'
                    AnyOf {
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2008-R2-SP1*'
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2012-*'
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2016-*'
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2019-*'
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2022-*'
                        Field 'Microsoft.Compute/imageSku' | Must -BeLike '2025-*'
                    }
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftWindowsServer'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'WindowsServerSemiAnnual'
                    Field 'Microsoft.Compute/imageSKU' | Must -BeIn @('Datacenter-Core-1709-smalldisk', 'Datacenter-Core-1709-with-Containers-smalldisk', 'Datacenter-Core-1803-with-Containers-smalldisk')
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftWindowsServerHPCPack'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'WindowsServerHPCPack'
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftSQLServer'
                    AnyOf {
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2025'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2025-BYOL'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2022'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2022-BYOL'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2019'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2019-BYOL'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2016'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2016-BYOL'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2012R2'
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike '*-WS2012R2-BYOL'
                    }
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftRServer'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'MLServer-WS2016'
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftVisualStudio'
                    Field 'Microsoft.Compute/imageOffer' | Must -BeIn @('VisualStudio', 'Windows')
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftDynamicsAX'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'Dynamics'
                    Field 'Microsoft.Compute/imageSKU' | Must -Be 'Pre-Req-AX7-Onebox-U8'
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'microsoft-ads'
                    Field 'Microsoft.Compute/imageOffer' | Must -Be 'windows-data-science-vm'
                }
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftWindowsDesktop'
                    Field 'Microsoft.Compute/imageOffer' | Must -BeLike 'Windows-1*'
                }
            }
        }

        Require 'identity.type does not contain SystemAssigned' {
            Field identity.type | Must -Not -ContainText 'SystemAssigned' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachineScaleSets/extensions' -Role 'Virtual Machine Contributor' -Effect (Param Effect) {
        Require 'type, publisher and provisioningState are as required' {
            Field 'Microsoft.Compute/virtualMachineScaleSets/extensions/type' | Must -Be 'AzureMonitorWindowsAgent'
            Field publisher | Must -Be 'Microsoft.Azure.Monitor'
            Field provisioningState | Must -Be 'Succeeded'
        }
        Deployment -TemplateFile './templates/4efbd9d8-6bc6-45f6-9be2-7fe9dd5d89ff.json' -Parameters ([ordered]@{
                vmName   = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Monitoring/AzureMonitor_Agent_Windows_VMSS_DINE.json in Azure/azure-policy, under the MIT licence.