Bylaw

Gallery › Guest Configuration › Azure Landing Zones Library

Deploy Windows Domain Join Extension with keyvault configuration

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.1.0

Deploy Windows Domain Join Extension with keyvault configuration when the extension does not exist on a given windows Virtual Machine

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

Azure Policy JSONDeploy-Windows-DomainJoin.alz_policy_definition.json · 226 lines
{
  "name": "Deploy-Windows-DomainJoin",
  "properties": {
    "description": "Deploy Windows Domain Join Extension with keyvault configuration when the extension does not exist on a given windows Virtual Machine",
    "displayName": "Deploy Windows Domain Join Extension with keyvault configuration",
    "metadata": {
      "alzCloudEnvironments": [
        "AzureCloud",
        "AzureChinaCloud",
        "AzureUSGovernment"
      ],
      "category": "Guest Configuration",
      "source": "https://github.com/Azure/Enterprise-Scale/",
      "version": "1.1.0"
    },
    "mode": "Indexed",
    "parameters": {
      "domainFQDN": {
        "metadata": {
          "displayName": "domainFQDN"
        },
        "type": "String"
      },
      "domainOUPath": {
        "metadata": {
          "displayName": "domainOUPath"
        },
        "type": "String"
      },
      "domainPassword": {
        "metadata": {
          "displayName": "domainPassword"
        },
        "type": "String"
      },
      "domainUsername": {
        "metadata": {
          "displayName": "domainUsername"
        },
        "type": "String"
      },
      "effect": {
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists",
        "metadata": {
          "description": "Enable or disable the execution of the policy",
          "displayName": "Effect"
        },
        "type": "String"
      },
      "keyVaultResourceId": {
        "metadata": {
          "displayName": "keyVaultResourceId"
        },
        "type": "String"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "equals": "Microsoft.Compute/virtualMachines",
            "field": "type"
          },
          {
            "equals": "MicrosoftWindowsServer",
            "field": "Microsoft.Compute/imagePublisher"
          },
          {
            "equals": "WindowsServer",
            "field": "Microsoft.Compute/imageOffer"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2008-R2-SP1*"
              },
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2012-*"
              },
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2016-*"
              },
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2019-*"
              },
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2022-*"
              },
              {
                "field": "Microsoft.Compute/imageSku",
                "like": "2025-*"
              }
            ]
          }
        ]
      },
      "then": {
        "details": {
          "deployment": {
            "properties": {
              "mode": "Incremental",
              "parameters": {
                "domainFQDN": {
                  "value": "[parameters('domainFQDN')]"
                },
                "domainOUPath": {
                  "value": "[parameters('domainOUPath')]"
                },
                "domainPassword": {
                  "reference": {
                    "keyVault": {
                      "id": "[parameters('keyVaultResourceId')]"
                    },
                    "secretName": "[parameters('domainPassword')]"
                  }
                },
                "domainUsername": {
                  "reference": {
                    "keyVault": {
                      "id": "[parameters('keyVaultResourceId')]"
                    },
                    "secretName": "[parameters('domainUsername')]"
                  }
                },
                "keyVaultResourceId": {
                  "value": "[parameters('keyVaultResourceId')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "vmName": {
                  "value": "[field('name')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "outputs": {},
                "parameters": {
                  "domainFQDN": {
                    "type": "String"
                  },
                  "domainOUPath": {
                    "type": "String"
                  },
                  "domainPassword": {
                    "type": "securestring"
                  },
                  "domainUsername": {
                    "type": "String"
                  },
                  "keyVaultResourceId": {
                    "type": "String"
                  },
                  "location": {
                    "type": "String"
                  },
                  "vmName": {
                    "type": "String"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2015-06-15",
                    "location": "[resourceGroup().location]",
                    "name": "[concat(variables('vmName'),'/joindomain')]",
                    "properties": {
                      "autoUpgradeMinorVersion": true,
                      "protectedSettings": {
                        "Password": "[parameters('domainPassword')]"
                      },
                      "publisher": "Microsoft.Compute",
                      "settings": {
                        "Name": "[parameters('domainFQDN')]",
                        "OUPath": "[parameters('domainOUPath')]",
                        "Options": "[variables('domainJoinOptions')]",
                        "Restart": "true",
                        "User": "[parameters('domainUserName')]"
                      },
                      "type": "JsonADDomainExtension",
                      "typeHandlerVersion": "1.3"
                    },
                    "type": "Microsoft.Compute/virtualMachines/extensions"
                  }
                ],
                "variables": {
                  "domainJoinOptions": 3,
                  "vmName": "[parameters('vmName')]"
                }
              }
            }
          },
          "existenceCondition": {
            "allOf": [
              {
                "equals": "JsonADDomainExtension",
                "field": "Microsoft.Compute/virtualMachines/extensions/type"
              },
              {
                "equals": "Microsoft.Compute",
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c",
            "/providers/Microsoft.Authorization/roleDefinitions/4633458b-17de-408a-b874-0445c86b69e6"
          ],
          "type": "Microsoft.Compute/virtualMachines/extensions"
        },
        "effect": "[parameters('effect')]"
      }
    },
    "policyType": "Custom"
  },
  "type": "Microsoft.Authorization/policyDefinitions"
}
BylawDeploy-Windows-DomainJoin.bylaw.ps1 · 87 lines
# Converted from Deploy-Windows-DomainJoin.alz_policy_definition.json by ConvertTo-Bylaw.
Policy 'Deploy-Windows-DomainJoin' {

    Properties @{
        DisplayName = 'Deploy Windows Domain Join Extension with keyvault configuration'
        Description = 'Deploy Windows Domain Join Extension with keyvault configuration when the extension does not exist on a given windows Virtual Machine'
        Category    = 'Guest Configuration'
        Version     = '1.1.0'
        Mode        = 'Indexed'
        Metadata    = [ordered]@{ alzCloudEnvironments = @('AzureCloud', 'AzureChinaCloud', 'AzureUSGovernment'); source = 'https://github.com/Azure/Enterprise-Scale/' }
    }

    Parameter DomainFQDN @{
        Type        = 'String'
        DisplayName = 'domainFQDN'
    }
    Parameter DomainOUPath @{
        Type        = 'String'
        DisplayName = 'domainOUPath'
    }
    Parameter DomainPassword @{
        Type        = 'String'
        DisplayName = 'domainPassword'
    }
    Parameter DomainUsername @{
        Type        = 'String'
        DisplayName = 'domainUsername'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter KeyVaultResourceId @{
        Type        = 'String'
        DisplayName = 'keyVaultResourceId'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field 'Microsoft.Compute/imagePublisher' | Must -Be 'MicrosoftWindowsServer'
            Field 'Microsoft.Compute/imageOffer' | Must -Be 'WindowsServer'
        }

        Require 'imageSku is not like 2008-R2-SP1*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2008-R2-SP1*' -IfPresent
        }

        Require 'imageSku is not like 2012-*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2012-*' -IfPresent
        }

        Require 'imageSku is not like 2016-*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2016-*' -IfPresent
        }

        Require 'imageSku is not like 2019-*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2019-*' -IfPresent
        }

        Require 'imageSku is not like 2022-*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2022-*' -IfPresent
        }

        Require 'imageSku is not like 2025-*' {
            Field 'Microsoft.Compute/imageSku' | Must -Not -BeLike '2025-*' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Virtual Machine Contributor', 'Key Vault Secrets User' -Effect (Param Effect) {
        Require 'type is JsonADDomainExtension and publisher is Microsoft.Compute' {
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'JsonADDomainExtension'
            Field publisher | Must -Be 'Microsoft.Compute'
        }
        Deployment -TemplateFile './templates/Deploy-Windows-DomainJoin.json' -Parameters ([ordered]@{
                domainFQDN         = (Param DomainFQDN)
                domainOUPath       = (Param DomainOUPath)
                domainPassword     = (KeyVaultSecret -VaultId (Param KeyVaultResourceId) -SecretName (Param DomainPassword))
                domainUsername     = (KeyVaultSecret -VaultId (Param KeyVaultResourceId) -SecretName (Param DomainUsername))
                keyVaultResourceId = (Param KeyVaultResourceId)
                location           = (Arm '[field(''location'')]')
                vmName             = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policy_definitions/Deploy-Windows-DomainJoin.alz_policy_definition.json in Azure/Azure-Landing-Zones-Library, under the MIT licence.