{
   "properties": {
      "displayName": "Deploy default Log Analytics VM Extension for Windows VMs.",
      "mode": "Indexed",
      "description": "This policy deploys Log Analytics VM Extensions on Windows VMs, and connects to the selected Log Analytics workspace.",
      "metadata": {
         "category": "Compute"
      },
      "parameters": {
         "logAnalytics": {
            "type": "String",
            "metadata": {
               "displayName": "Log Analytics workspace",
               "description": "Select Log Analytics workspace from dropdown list",
               "strongType": "omsWorkspace"
            }
         }
      },
      "policyRule": {
         "if": {
            "allOf": [
               {
                  "field": "type",
                  "equals": "Microsoft.Compute/virtualMachines"
               },
               {
                  "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"
                  ]
               }
            ]
         },
         "then": {
            "effect": "deployIfNotExists",
            "details": {
               "type": "Microsoft.Compute/virtualMachines/extensions",
               "existenceCondition": {
                  "allOf": [
                     {
                        "field": "Microsoft.Compute/virtualMachines/extensions/type",
                        "equals": "MicrosoftMonitoringAgent"
                     },
                     {
                        "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                        "equals": "Microsoft.EnterpriseCloud.Monitoring"
                     }
                  ]
               },
               "roleDefinitionIds": [
                  "/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
               ],
               "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"
                           },
                           "logAnalytics": {
                              "type": "string"
                           }
                        },
                        "resources": [
                           {
                              "name": "[concat(parameters('vmName'),'/omsPolicy')]",
                              "type": "Microsoft.Compute/virtualMachines/extensions",
                              "location": "[parameters('location')]",
                              "apiVersion": "2017-12-01",
                              "properties": {
                                 "publisher": "Microsoft.EnterpriseCloud.Monitoring",
                                 "type": "MicrosoftMonitoringAgent",
                                 "typeHandlerVersion": "1.0",
                                 "autoUpgradeMinorVersion": true,
                                 "settings": {
                                    "workspaceId": "[reference(parameters('logAnalytics'), '2015-03-20').customerId]"
                                 },
                                 "protectedSettings": {
                                    "workspaceKey": "[listKeys(parameters('logAnalytics'), '2015-03-20').primarySharedKey]"
                                 }
                              }
                           }
                        ],
                        "outputs": {
                           "policy": {
                              "type": "string",
                              "value": "[concat('Enabled monitoring for Windows VM', ': ', parameters('vmName'))]"
                           }
                        }
                     },
                     "parameters": {
                        "vmName": {
                           "value": "[field('name')]"
                        },
                        "location": {
                           "value": "[field('location')]"
                        },
                        "logAnalytics": {
                           "value": "[parameters('logAnalytics')]"
                        }
                     }
                  }
               }
            }
         }
      }
   }
}