Bylaw

Gallery › Monitoring › Azure built-in policies

Windows virtual machines should have Azure Monitor Agent installed

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 3.5.0

Windows virtual machines should be monitored and secured through the deployed Azure Monitor Agent. The Azure Monitor Agent collects telemetry data from the guest OS. Windows virtual machines with supported OS and in supported regions are monitored for Azure Monitor Agent deployment. 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_VM_Audit.json · 372 lines
{
  "properties": {
    "displayName": "Windows virtual machines should have Azure Monitor Agent installed",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Windows virtual machines should be monitored and secured through the deployed Azure Monitor Agent. The Azure Monitor Agent collects telemetry data from the guest OS. Windows virtual machines with supported OS and in supported regions are monitored for Azure Monitor Agent deployment. Learn more: https://aka.ms/AMAOverview.",
    "metadata": {
      "version": "3.5.0",
      "category": "Monitoring"
    },
    "version": "3.5.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy."
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "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 machines with supported operating systems. Otherwise, the policy will apply to all virtual machine 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/virtualMachines"
          },
          {
            "field": "location",
            "in": "[parameters('listOfApplicableLocations')]"
          },
          {
            "anyOf": [
              {
                "allOf": [
                  {
                    "value": "[parameters('scopeToSupportedImages')]",
                    "equals": false
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/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",
                    "equals": "Windows-10"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "AzureMonitorWindowsAgent"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "Microsoft.Azure.Monitor"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          }
        }
      }
    },
    "versions": [
      "3.5.0",
      "3.4.0",
      "3.3.0",
      "3.2.0",
      "3.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/c02729e5-e5e7-4458-97fa-2b5ad0661f28",
  "name": "c02729e5-e5e7-4458-97fa-2b5ad0661f28"
}
Bylawc02729e5-e5e7-4458-97fa-2b5ad0661f28.bylaw.ps1 · 149 lines
# Converted from AzureMonitor_Agent_Windows_VM_Audit.json by ConvertTo-Bylaw.
Policy 'c02729e5-e5e7-4458-97fa-2b5ad0661f28' {

    Properties @{
        DisplayName   = 'Windows virtual machines should have Azure Monitor Agent installed'
        Description   = 'Windows virtual machines should be monitored and secured through the deployed Azure Monitor Agent. The Azure Monitor Agent collects telemetry data from the guest OS. Windows virtual machines with supported OS and in supported regions are monitored for Azure Monitor Agent deployment. Learn more: https://aka.ms/AMAOverview.'
        Version       = '3.5.0'
        Category      = 'Monitoring'
        PolicyVersion = '3.5.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        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 machines with supported operating systems. Otherwise, the policy will apply to all virtual machine 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/virtualMachines' {

        When {
            Field location | Must -BeIn (Param ListOfApplicableLocations)
        }

        Require 'storageProfile.osDisk.osType is as required' {
            AnyOf {
                Param ScopeToSupportedImages | Must -Not -Be $false
                Field storageProfile.osDisk.osType | Must -Not -BeLike 'Windows*' -IfPresent
            }
        }

        Require 'imageId is not one of the ListOfWindowsImageIdToInclude parameter' {
            Field 'Microsoft.Compute/imageId' | Must -Not -BeIn (Param ListOfWindowsImageIdToInclude) -IfPresent
        }

        Require 'imagePublisher, imageOffer and imageSku are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftWindowsServer' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'WindowsServer' -IfPresent
                AllOf {
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2008-R2-SP1*' -IfPresent
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2012-*' -IfPresent
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2016-*' -IfPresent
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2019-*' -IfPresent
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2022-*' -IfPresent
                    Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2025-*' -IfPresent
                }
            }
        }

        Require 'imagePublisher, imageOffer and imageSKU are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftWindowsServer' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'WindowsServerSemiAnnual' -IfPresent
                Field 'Microsoft.Compute/imageSKU' | Must -Not -BeIn @('Datacenter-Core-1709-smalldisk', 'Datacenter-Core-1709-with-Containers-smalldisk', 'Datacenter-Core-1803-with-Containers-smalldisk') -IfPresent
            }
        }

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

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftSQLServer' -IfPresent
                AllOf {
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2025' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2025-BYOL' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2022' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2022-BYOL' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2019' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2019-BYOL' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2016' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2016-BYOL' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2012R2' -IfPresent
                    Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike '*-WS2012R2-BYOL' -IfPresent
                }
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftRServer' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'MLServer-WS2016' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftVisualStudio' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -BeIn @('VisualStudio', 'Windows') -IfPresent
            }
        }

        Require 'imagePublisher, imageOffer and imageSKU are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftDynamicsAX' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'Dynamics' -IfPresent
                Field 'Microsoft.Compute/imageSKU' | Must -Not -Be 'Pre-Req-AX7-Onebox-U8' -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 -Be 'windows-data-science-vm' -IfPresent
            }
        }

        Require 'imagePublisher and imageOffer are as required' {
            AnyOf {
                Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftWindowsDesktop' -IfPresent
                Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'Windows-10' -IfPresent
            }
        }
    }

    AuditIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Effect (Param Effect) {
        Require 'type, publisher and provisioningState are as required' {
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'AzureMonitorWindowsAgent'
            Field publisher | Must -Be 'Microsoft.Azure.Monitor'
            Field provisioningState | Must -Be 'Succeeded'
        }
    }
}

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