{
  "properties": {
    "displayName": "Deploy DSC Extension to Azure VM and Arc connected machines",
    "mode": "Indexed",
    "description": "Deploys the DSC extension to and assigns configuration artifact from url location.",
    "metadata": {
      "category": "Automation"
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "tags['dsc']",
            "equals": "true"
          },
          {
            "anyOf": [
              {
                "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",
                            "equals": "dsvm-windows"
                          }
                        ]
                      },
                      {
                        "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*"
                                  }
                                ]
                              }
                            ]
                          }
                        ]
                      }
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.HybridCompute/machines"
                  },
                  {
                    "field": "Microsoft.HybridCompute/imageOffer",
                    "like": "windows*"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "deployIfNotExists",
        "details": {
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "type": "[if(equals(tolower(field('Type')),'microsoft.compute/virtualmachines'),'Microsoft.Compute/virtualMachines/extensions','Microsoft.HybridCompute/machines/extensions')]",
          "name": "DSC",
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "configurationUrl": {
                  "value": "[parameters('configurationUrl')]"
                },
                "configurationScript": {
                  "value": "[parameters('configurationScript')]"
                },
                "configurationFunction": {
                  "value": "[parameters('configurationFunction')]"
                },
                "machineName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "type": {
                  "value": "[field('type')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "configurationUrl": {
                    "type": "String"
                  },
                  "configurationScript": {
                    "type": "String"
                  },
                  "configurationFunction": {
                    "type": "String"
                  },
                  "machineName": {
                    "type": "String"
                  },
                  "location": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "condition": "[equals(toLower(parameters('type')), toLower('Microsoft.Compute/virtualMachines'))]",
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "name": "[concat(parameters('machineName'),'/DSC')]",
                    "apiVersion": "2019-07-01",
                    "location": "[parameters('location')]",
                    "properties": {
                      "publisher": "Microsoft.Powershell",
                      "type": "DSC",
                      "typeHandlerVersion": "2.80",
                      "autoUpgradeMinorVersion": true,
                      "settings": {
                        "configuration": {
                          "url": "[parameters('configurationUrl')]",
                          "script": "[parameters('configurationScript')]",
                          "function": "[parameters('configurationFunction')]"
                        }
                      }
                    }
                  },
                  {
                    "condition": "[equals(toLower(parameters('type')), toLower('microsoft.hybridcompute/machines'))]",
                    "type": "Microsoft.HybridCompute/machines/extensions",
                    "name": "[concat(parameters('machineName'),'/DSC')]",
                    "apiVersion": "2019-12-12",
                    "location": "[parameters('location')]",
                    "properties": {
                      "publisher": "Microsoft.Powershell",
                      "type": "DSC",
                      "typeHandlerVersion": "2.80",
                      "autoUpgradeMinorVersion": true,
                      "settings": {
                        "configuration": {
                          "url": "[parameters('configurationUrl')]",
                          "script": "[parameters('configurationScript')]",
                          "function": "[parameters('configurationFunction')]"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  },
  "parameters": {
    "configurationUrl": {
      "type": "String",
      "metadata": {
        "displayName": "Configuration URL",
        "description": "Https location of the .zip file artifact containing the DSC configuration script and dependencies"
      }
    },
    "configurationScript": {
      "type": "String",
      "metadata": {
        "displayName": "Configuration script",
        "description": "The name of the .ps1 script file inside the .zip artifact"
      }
    },
    "configurationFunction": {
      "type": "String",
      "metadata": {
        "displayName": "Configuration name",
        "description": "The name of the configuration provided in the script file inside the .zip artifact"
      }
    }
  }
}