Bylaw

Gallery › Monitoring › Azure Community Policy

Deploy Log Analytics agent dynamically to different regions for Windows VMs

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploy Log Analytics agent for Windows VMs if the VM Image (OS) is in the list defined and the agent is not installed. The list of OS images will be updated over time as support is updated. This Policy is built to be able to connect a VM to a Workspace in the same region as it. This requires that all of the workspaces be deployed into the same subscription, have the same resource group naming convention, and have the same workspace naming convention.

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

Azure Policy JSONazurepolicy.json · 337 lines
{
  "name": "30747f84-6390-43b0-8526-8c91f94a04b7",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Log Analytics agent dynamically to different regions for Windows VMs",
    "description": "Deploy Log Analytics agent for Windows VMs if the VM Image (OS) is in the list defined and the agent is not installed. The list of OS images will be updated over time as support is updated. This Policy is built to be able to connect a VM to a Workspace in the same region as it. This requires that all of the workspaces be deployed into the same subscription, have the same resource group naming convention, and have the same workspace naming convention.",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "mode": "Indexed",
    "parameters": {
      "logAnalyticsSubscriptionId": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace subscription id",
          "description": "Subscription ID where all of your workspaces reside."
        }
      },
      "logAnalyticsResourceGroupNamingConvention": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace resource group naming convention",
          "description": "This works using the following concactenation: (concat(parameters('logAnalyticsResourceGroupNamingConvention'), field('location')). This means that all of your resource groups need to follow this naming pattern (example given is for east us): YOUR-NAMING-CONVENTION-EASTUS."
        }
      },
      "logAnalyticsNamingConvention": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics workspace naming convention",
          "description": "This works using the following concactenation: (concat(parameters('logAnalyticsNamingConvention'), field('location')). This means that all of your workspaces need to follow this naming pattern (example given is for east us): YOUR-NAMING-CONVENTION-EASTUS."
        }
      },
      "listOfImageIdToInclude": {
        "type": "Array",
        "metadata": {
          "displayName": "Optional: List of VM images that have supported Windows OS to add to scope",
          "description": "Example values: '/subscriptions/<subscriptionId>/resourceGroups/YourResourceGroup/providers/Microsoft.Compute/images/ContosoStdImage'"
        },
        "defaultValue": []
      },
      "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/virtualMachineScaleSets"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Compute/imageId",
                "in": "[parameters('listOfImageIdToInclude')]"
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "equals": "MicrosoftWindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageOffer",
                    "equals": "WindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/imageSKU",
                    "in": [
                      "2008-R2-SP1",
                      "2008-R2-SP1-smalldisk",
                      "2012-Datacenter",
                      "2012-Datacenter-smalldisk",
                      "2012-R2-Datacenter",
                      "2012-R2-Datacenter-smalldisk",
                      "2016-Datacenter",
                      "2016-Datacenter-Server-Core",
                      "2016-Datacenter-Server-Core-smalldisk",
                      "2016-Datacenter-smalldisk",
                      "2016-Datacenter-with-Containers",
                      "2016-Datacenter-with-RDSH",
                      "2019-Datacenter",
                      "2019-Datacenter-Core",
                      "2019-Datacenter-Core-smalldisk",
                      "2019-Datacenter-Core-with-Containers",
                      "2019-Datacenter-Core-with-Containers-smalldisk",
                      "2019-Datacenter-smalldisk",
                      "2019-Datacenter-with-Containers",
                      "2019-Datacenter-with-Containers-smalldisk",
                      "2019-Datacenter-zhcn"
                    ]
                  }
                ]
              },
              {
                "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": "*-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/virtualMachineScaleSets/extensions",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/type",
                "equals": "MicrosoftMonitoringAgent"
              },
              {
                "field": "Microsoft.Compute/virtualMachineScaleSets/extensions/publisher",
                "equals": "Microsoft.EnterpriseCloud.Monitoring"
              }
            ]
          },
          "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"
                  },
                  "location": {
                    "type": "string"
                  },
                  "logAnalyticsSubscriptionId": {
                    "type": "string"
                  },
                  "logAnalyticsResourceGroupNamingConvention": {
                    "type": "string"
                  },
                  "logAnalyticsNamingConvention": {
                    "type": "string"
                  }
                },
                "variables": {
                  "vmExtensionName": "MMAExtension",
                  "vmExtensionPublisher": "Microsoft.EnterpriseCloud.Monitoring",
                  "vmExtensionType": "MicrosoftMonitoringAgent",
                  "vmExtensionTypeHandlerVersion": "1.0"
                },
                "resources": [
                  {
                    "name": "[concat(parameters('vmName'), '/', variables('vmExtensionName'))]",
                    "type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
                    "location": "[parameters('location')]",
                    "apiVersion": "2018-06-01",
                    "properties": {
                      "publisher": "[variables('vmExtensionPublisher')]",
                      "type": "[variables('vmExtensionType')]",
                      "typeHandlerVersion": "[variables('vmExtensionTypeHandlerVersion')]",
                      "autoUpgradeMinorVersion": true,
                      "settings": {
                        "workspaceId": "[reference(concat('/subscriptions/', parameters('logAnalyticsSubscriptionId'), '/resourcegroups/', parameters('logAnalyticsResourceGroupNamingConvention'), parameters('location'), '/providers/microsoft.operationalinsights/workspaces/', parameters('logAnalyticsNamingConvention'), parameters('location')), '2015-03-20').customerId]",
                        "stopOnMultipleConnections": "true"
                      },
                      "protectedSettings": {
                        "workspaceKey": "[listKeys(concat('/subscriptions/', parameters('logAnalyticsSubscriptionId'), '/resourcegroups/', parameters('logAnalyticsResourceGroupNamingConvention'), parameters('location'), '/providers/microsoft.operationalinsights/workspaces/', parameters('logAnalyticsNamingConvention'), parameters('location')), '2015-03-20').primarySharedKey]"
                      }
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Enabled extension for VM', ': ', parameters('vmName'))]"
                  }
                }
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "logAnalyticsSubscriptionId": {
                  "value": "[parameters('logAnalyticsSubscriptionId')]"
                },
                "logAnalyticsResourceGroupNamingConvention": {
                  "value": "[parameters('logAnalyticsResourceGroupNamingConvention')]"
                },
                "logAnalyticsNamingConvention": {
                  "value": "[parameters('logAnalyticsNamingConvention')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw30747f84-6390-43b0-8526-8c91f94a04b7.bylaw.ps1 · 131 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '30747f84-6390-43b0-8526-8c91f94a04b7' {

    Properties @{
        DisplayName = 'Deploy Log Analytics agent dynamically to different regions for Windows VMs'
        Description = 'Deploy Log Analytics agent for Windows VMs if the VM Image (OS) is in the list defined and the agent is not installed. The list of OS images will be updated over time as support is updated. This Policy is built to be able to connect a VM to a Workspace in the same region as it. This requires that all of the workspaces be deployed into the same subscription, have the same resource group naming convention, and have the same workspace naming convention.'
        Version     = '1.0.0'
        Category    = 'Monitoring'
        Mode        = 'Indexed'
    }

    Parameter LogAnalyticsSubscriptionId @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace subscription id'
        Description = 'Subscription ID where all of your workspaces reside.'
    }
    Parameter LogAnalyticsResourceGroupNamingConvention @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace resource group naming convention'
        Description = 'This works using the following concactenation: (concat(parameters(''logAnalyticsResourceGroupNamingConvention''), field(''location'')). This means that all of your resource groups need to follow this naming pattern (example given is for east us): YOUR-NAMING-CONVENTION-EASTUS.'
    }
    Parameter LogAnalyticsNamingConvention @{
        Type        = 'String'
        DisplayName = 'Log Analytics workspace naming convention'
        Description = 'This works using the following concactenation: (concat(parameters(''logAnalyticsNamingConvention''), field(''location'')). This means that all of your workspaces need to follow this naming pattern (example given is for east us): YOUR-NAMING-CONVENTION-EASTUS.'
    }
    Parameter ListOfImageIdToInclude @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Optional: List of VM images that have supported Windows OS to add to scope'
        Description = 'Example values: ''/subscriptions/<subscriptionId>/resourceGroups/YourResourceGroup/providers/Microsoft.Compute/images/ContosoStdImage'''
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachineScaleSets' {

        Require 'imageId is not one of the ListOfImageIdToInclude parameter' {
            Field 'Microsoft.Compute/imageId' | Must -Not -BeIn (Param ListOfImageIdToInclude) -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
                Field 'Microsoft.Compute/imageSKU' | Must -Not -BeIn @('2008-R2-SP1', '2008-R2-SP1-smalldisk', '2012-Datacenter', '2012-Datacenter-smalldisk', '2012-R2-Datacenter', '2012-R2-Datacenter-smalldisk', '2016-Datacenter', '2016-Datacenter-Server-Core', '2016-Datacenter-Server-Core-smalldisk', '2016-Datacenter-smalldisk', '2016-Datacenter-with-Containers', '2016-Datacenter-with-RDSH', '2019-Datacenter', '2019-Datacenter-Core', '2019-Datacenter-Core-smalldisk', '2019-Datacenter-Core-with-Containers', '2019-Datacenter-Core-with-Containers-smalldisk', '2019-Datacenter-smalldisk', '2019-Datacenter-with-Containers', '2019-Datacenter-with-Containers-smalldisk', '2019-Datacenter-zhcn') -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 '*-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
            }
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachineScaleSets/extensions' -Role 'Log Analytics Contributor' -Effect (Param Effect) {
        Require 'type is MicrosoftMonitoringAgent and publisher is Microsoft.EnterpriseCloud.Monitoring' {
            Field 'Microsoft.Compute/virtualMachineScaleSets/extensions/type' | Must -Be 'MicrosoftMonitoringAgent'
            Field publisher | Must -Be 'Microsoft.EnterpriseCloud.Monitoring'
        }
        Deployment -TemplateFile './templates/30747f84-6390-43b0-8526-8c91f94a04b7.json' -Parameters ([ordered]@{
                vmName                                    = (Arm '[field(''name'')]')
                location                                  = (Arm '[field(''location'')]')
                logAnalyticsSubscriptionId                = (Param LogAnalyticsSubscriptionId)
                logAnalyticsResourceGroupNamingConvention = (Param LogAnalyticsResourceGroupNamingConvention)
                logAnalyticsNamingConvention              = (Param LogAnalyticsNamingConvention)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/deploy-log-analytics-agent-dynamically-to-different-regions-for-windows-vms/azurepolicy.json in Azure/Community-Policy, under the MIT licence.