{
  "properties": {
    "displayName": "Onboard Azure VM and Arc connected machines to Azure Automation DSC",
    "mode": "Indexed",
    "description": "Deploys the DSC extension to onboard nodes to Azure Automation DSC. Does not assign a configuration.",
    "metadata": {
      "category": "Automation"
    },
    "policyRule": {
      "if": {
        "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": {
                "automationAccount": {
                  "value": "[parameters('automationAccount')]"
                },
                "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": {
                  "machineName": {
                    "type": "String"
                  },
                  "automationAccount": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  }
                },
                "variables": {
                  "automationAccountName": "[last(split(parameters('automationAccount'),'/'))]"
                },
                "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,
                      "protectedSettings": {
                        "Items": {
                          "registrationKeyPrivate": "[listKeys(parameters('automationAccount'), '2018-01-15').Keys[0].value]"
                        }
                      },
                      "settings": {
                        "Properties": [
                          {
                            "Name": "RegistrationKey",
                            "Value": {
                              "UserName": "PLACEHOLDER_DONOTUSE",
                              "Password": "PrivateSettingsRef:registrationKeyPrivate"
                            },
                            "TypeName": "System.Management.Automation.PSCredential"
                          },
                          {
                            "Name": "RegistrationUrl",
                            "Value": "[reference(parameters('automationAccount'),'2018-01-15').registrationUrl]",
                            "TypeName": "System.String"
                          }
                        ]
                      }
                    }
                  },
                  {
                    "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,
                      "protectedSettings": {
                        "Items": {
                          "registrationKeyPrivate": "[listKeys(parameters('automationAccount'), '2018-01-15').Keys[0].value]"
                        }
                      },
                      "settings": {
                        "Properties": [
                          {
                            "Name": "RegistrationKey",
                            "Value": {
                              "UserName": "PLACEHOLDER_DONOTUSE",
                              "Password": "PrivateSettingsRef:registrationKeyPrivate"
                            },
                            "TypeName": "System.Management.Automation.PSCredential"
                          },
                          {
                            "Name": "RegistrationUrl",
                            "Value": "[reference(parameters('automationAccount'),'2018-01-15').registrationUrl]",
                            "TypeName": "System.String"
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "parameters": {
      "automationAccount": {
        "type": "String",
        "metadata": {
          "displayName": "Automation account",
          "description": "Select Automation account from dropdown list. If this account is outside of the scope of the assignment you must manually grant 'Contributor' permissions (or similar) on the Automation account to the policy assignment's principal ID.",
          "strongType": "Microsoft.Automation/automationAccounts",
          "assignPermissions": true
        }
      }
    }
  }
}