How to be required in Azure custom policy to have to install Virtual Machine Custom Extension or CustomScriptExtension

Oda, Yutaka 171 Reputation points
2023-07-07T05:01:39.1333333+00:00

Hi, community!

Currently, I'm struggling to create Azure custom policy requiring VM to install Virtual Machine Custom Extension or CustomScriptExtension.

And I create Azure Policy but I can't understand how specify custom extension which must be installed.

I can't find rerated parameter or property.

Could you give me advice?

{
  "properties": {
    "displayName": "Require Installation of Custom Extension on VMs",
    "policyType": "Custom",
    "mode": "Indexed",
    "description": "This policy ensures that a custom extension is installed on VMs.",
    "metadata": {
      "version": "1.0.0",
      "category": "CustomExtensions"
    },
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Specifies the effect of the policy enforcement."
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "listOfImageIdToInclude": {
        "type": "Array",
        "metadata": {
          "displayName": "List of Image IDs to Include",
          "description": "Specifies the list of image IDs to include in the policy."
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Compute/imageId",
                "in": "[parameters('listOfImageIdToInclude')]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "CustomScriptExtension"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                "equals": "Succeeded"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/settings.workspaceId",
                "exists": "true"
              }
            ],
            "deployment": {
              "type": "Microsoft.Compute/virtualMachines/extensions",
              "apiVersion": "2021-04-01",
              "name": "[format('{0}/{1}', field('name'), 'InstallWebServer')]",
              "location": "[field('location')]",
              "properties": {
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "typeHandlerVersion": "1.7",
                "autoUpgradeMinorVersion": true,
                "settings": {
                  "fileUris": [
                    "https://azureblobstorageurl"
                  ],
                  "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File DiskPartitionChangePS.ps1"
                },
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Enabled extension for VM: ', field('name'))]"
                  }
                }
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,044 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,019 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Oda, Yutaka 171 Reputation points
    2023-07-18T05:39:39.6566667+00:00

    I built the Custom Extension(CustomScriptExtension) Policy.

    {
      "mode": "All",
      "policyRule": {
        "if": {
          "allOf": [
            {
              "field": "type",
              "equals": "Microsoft.Compute/virtualMachines"
            },
            {
              "anyOf": [
                {
                  "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
                  "equals": "Windows"
                },
                {
                  "field": "Microsoft.Compute/virtualMachines/osProfile.windowsConfiguration",
                  "exists": true
                }
              ]
            }
          ]
        },
        "then": {
          "effect": "deployIfNotExists",
          "details": {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "existenceCondition": {
              "field": "Microsoft.Compute/virtualMachines/extensions/type",
              "equals": "CustomScriptExtension"
            },
            "deployment": {
              "properties": {
                "mode": "incremental",
                "template": {
                  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                  "contentVersion": "1.0.0.0",
                  "parameters": {
                    "location": {
                      "type": "string"
                    },
                    "virtualMachineName": {
                      "type": "string"
                    }
                  },
                  "resources": [
                    {
                      "name": "[concat(parameters('virtualMachineName'),'/', 'CustomScriptExtension')]",
                      "type": "Microsoft.Compute/virtualMachines/extensions",
                      "apiVersion": "2023-03-01",
                      "location": "[parameters('location')]",
                      "properties": {
                        "publisher": "Microsoft.Compute",
                        "type": "CustomScriptExtension",
                        "typeHandlerVersion": "1.10",
                        "autoUpgradeMinorVersion": true,
                        "settings": {
                          "fileUris": [
                          "Azure blob file path"
                          ],
                          "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File PS.ps1"
                        },
                        "protectedSettings": {}
                      }
                    }
                  ]
                },
                "parameters": {
                  "location": {
                    "value": "[field('location')]"
                  },
                  "virtualMachineName": {
                    "value": "[field('name')]"
                  }
                }
              }
            },
            "roleDefinitionIds": [
              "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
            ]
          }
        }
      },
      "parameters": {}
    }
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.