Bylaw

Gallery › Compute › Azure Community Policy

Configure Windows virtual machines to use Microsoft Entra login

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Deploys the AADLoginForWindows extension to Windows virtual machines running a supported image, so users can sign in over RDP with Microsoft Entra credentials. Only virtual machines that already have a system-assigned managed identity are targeted, because the extension cannot install without one. Set matchAllWindowsImages to true for custom or golden images. Installing the extension joins the virtual machine to Microsoft Entra ID, which prevents an Active Directory domain join.

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

Azure Policy JSONazurepolicy.json · 189 lines
{
  "name": "304dff40-5056-4cbb-986c-58dc7edc8ee3",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Configure Windows virtual machines to use Microsoft Entra login",
    "description": "Deploys the AADLoginForWindows extension to Windows virtual machines running a supported image, so users can sign in over RDP with Microsoft Entra credentials. Only virtual machines that already have a system-assigned managed identity are targeted, because the extension cannot install without one. Set matchAllWindowsImages to true for custom or golden images. Installing the extension joins the virtual machine to Microsoft Entra ID, which prevents an Active Directory domain join.",
    "metadata": {
      "category": "Compute",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "matchAllWindowsImages": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Match all Windows images",
          "description": "Set to true to target every Windows virtual machine regardless of its image reference, for example when custom or golden images are used. Set to false to only target the marketplace images that Microsoft documents as supported."
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": false
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
            "equals": "Windows"
          },
          {
            "field": "identity.type",
            "contains": "SystemAssigned"
          },
          {
            "anyOf": [
              {
                "value": "[parameters('matchAllWindowsImages')]",
                "equals": true
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.publisher",
                    "equals": "MicrosoftWindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.offer",
                    "equals": "WindowsServer"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                    "notContains": "core"
                  },
                  {
                    "anyOf": [
                      {
                        "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                        "like": "2022-*"
                      },
                      {
                        "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                        "like": "2025-*"
                      }
                    ]
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.publisher",
                    "equals": "MicrosoftWindowsDesktop"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                    "like": "win11-*"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                    "notLike": "win11-21h2*"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                    "notLike": "win11-22h2*"
                  },
                  {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.imageReference.sku",
                    "notLike": "win11-23h2*"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "evaluationDelay": "AfterProvisioningSuccess",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "Microsoft.Azure.ActiveDirectory"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "AADLoginForWindows"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "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"
                  }
                },
                "resources": [
                  {
                    "name": "[concat(parameters('vmName'), '/AADLoginForWindows')]",
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "location": "[parameters('location')]",
                    "apiVersion": "2023-03-01",
                    "properties": {
                      "publisher": "Microsoft.Azure.ActiveDirectory",
                      "type": "AADLoginForWindows",
                      "typeHandlerVersion": "1.0",
                      "autoUpgradeMinorVersion": true
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Enabled Microsoft Entra login for Windows VM', ': ', parameters('vmName'))]"
                  }
                }
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw304dff40-5056-4cbb-986c-58dc7edc8ee3.bylaw.ps1 · 71 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '304dff40-5056-4cbb-986c-58dc7edc8ee3' {

    Properties @{
        DisplayName = 'Configure Windows virtual machines to use Microsoft Entra login'
        Description = 'Deploys the AADLoginForWindows extension to Windows virtual machines running a supported image, so users can sign in over RDP with Microsoft Entra credentials. Only virtual machines that already have a system-assigned managed identity are targeted, because the extension cannot install without one. Set matchAllWindowsImages to true for custom or golden images. Installing the extension joins the virtual machine to Microsoft Entra ID, which prevents an Active Directory domain join.'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }
    Parameter MatchAllWindowsImages @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $false
        DisplayName   = 'Match all Windows images'
        Description   = 'Set to true to target every Windows virtual machine regardless of its image reference, for example when custom or golden images are used. Set to false to only target the marketplace images that Microsoft documents as supported.'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field storageProfile.osDisk.osType | Must -Be 'Windows'
            Field identity.type | Must -ContainText 'SystemAssigned'
        }

        Require 'Requirement 1' {
            Param MatchAllWindowsImages | Must -Not -Be $true
        }

        Require 'storageProfile.imageReference.publisher, storageProfile.imageReference.offer and storageProfile.imageReference.sku are as required' {
            AnyOf {
                Field storageProfile.imageReference.publisher | Must -Not -Be 'MicrosoftWindowsServer' -IfPresent
                Field storageProfile.imageReference.offer | Must -Not -Be 'WindowsServer' -IfPresent
                Field storageProfile.imageReference.sku | Must -ContainText 'core'
                AllOf {
                    Field storageProfile.imageReference.sku | Must -Not -BeLike '2022-*' -IfPresent
                    Field storageProfile.imageReference.sku | Must -Not -BeLike '2025-*' -IfPresent
                }
            }
        }

        Require 'storageProfile.imageReference.publisher and storageProfile.imageReference.sku are as required' {
            AnyOf {
                Field storageProfile.imageReference.publisher | Must -Not -Be 'MicrosoftWindowsDesktop' -IfPresent
                Field storageProfile.imageReference.sku | Must -Not -BeLike 'win11-*' -IfPresent
                Field storageProfile.imageReference.sku | Must -BeLike 'win11-21h2*'
                Field storageProfile.imageReference.sku | Must -BeLike 'win11-22h2*'
                Field storageProfile.imageReference.sku | Must -BeLike 'win11-23h2*'
            }
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Virtual Machine Contributor' -EvaluationDelay AfterProvisioningSuccess -Effect (Param Effect) {
        Require 'publisher, type and provisioningState are as required' {
            Field publisher | Must -Be 'Microsoft.Azure.ActiveDirectory'
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'AADLoginForWindows'
            Field provisioningState | Must -Be 'Succeeded'
        }
        Deployment -TemplateFile './templates/304dff40-5056-4cbb-986c-58dc7edc8ee3.json' -Parameters ([ordered]@{
                vmName   = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Compute/configure-windows-virtual-machines-to-use-microsoft-entra-login/azurepolicy.json in Azure/Community-Policy, under the MIT licence.