Bylaw

Gallery › Compute › Azure Community Policy

Deploy Windows VM Application

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy will deploy a vm application to Windows virtual machines. You will need to assign the managed identity the reader role to the Azure Compute Gallery where the application resides. Users or service principles deploying virtual machines will also need the reader role assigned to the Azure Compute Gallery for the policy to take effect during new virtual machine deployments

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

Azure Policy JSONazurepolicy.json · 182 lines
{
	"properties": {
		"displayName": "Deploy Windows VM Application",
		"mode": "Indexed",
		"description": "This policy will deploy a vm application to Windows virtual machines. You will need to assign the managed identity the reader role to the Azure Compute Gallery where the application resides. Users or service principles deploying virtual machines will also need the reader role assigned to the Azure Compute Gallery for the policy to take effect during new virtual machine deployments",
		"metadata": {
			"category": "Compute",
			"version": "1.0.0"
		},
		"parameters": {
			"effect": {
				"type": "String",
				"metadata": {
					"displayName": "Effect",
					"description": "Enable or disable the execution of the policy."
				},
				"allowedValues": [
					"DeployIfNotExists",
					"AuditIfNotExists",
					"Disabled"
				],
				"defaultValue": "DeployIfNotExists"
			},
			"galleryApplication": {
				"type": "String",
				"metadata": {
					"displayName": "VM Application Version",
					"description": "The package reference id to vm application version to deploy",
					"strongType": "microsoft.compute/galleries/applications/versions",
					"portalReview": true
				}
			},
			"enableAutomaticUpgrade": {
				"type": "Boolean",
				"metadata": {
					"displayName": "Enable Automatic Upgrade",
					"description": "If set to true, when a new Gallery Application version is available it will be automatically updated",
					"portalReview": true
				},
				"defaultValue": true
			},
			"treatFailureAsDeploymentFailure": {
				"type": "Boolean",
				"metadata": {
					"displayName": "Treat Failure As Deployment Failure",
					"description": "Mark application failure as VM deployment failure for failure handling while being installed/updated/removed",
					"portalReview": true
				},
				"defaultValue": false
			},
			"configurationReference": {
				"type": "String",
				"metadata": {
					"displayName": "Configuration File Reference",
					"description": "Optional, Specifies the uri to an azure blob that will replace the default configuration for the package if provided",
					"portalReview": true
				},
				"defaultValue": ""
			}
		},
		"policyRule": {
			"if": {
				"allOf": [
					{
						"field": "type",
						"equals": "Microsoft.Compute/virtualMachines"
					},
					{
						"anyOf": [
							{
								"field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
								"contains": "Windows"
							},
							{
								"field": "Microsoft.Compute/imageOffer",
								"contains": "Windows"
							},
							{
								"field": "Microsoft.Compute/imagePublisher",
								"contains": "microsoftsqlserver"
							},
							{
								"field": "Microsoft.Compute/imagePublisher",
								"contains": "microsoftwindowsdesktop"
							}
						]
					}
				]
			},
			"then": {
				"effect": "[parameters('effect')]",
				"details": {
					"type": "Microsoft.Compute/virtualMachines",
					"name": "[field('name')]",
					"existenceCondition": {
						"allOf": [
							{
								"count": {
									"field": "Microsoft.Compute/virtualMachines/applicationProfile.galleryApplications[*]",
									"where": {
										"field": "Microsoft.Compute/virtualMachines/applicationProfile.galleryApplications[*].packageReferenceId",
										"equals": "[parameters('galleryApplication')]"
									}
								},
								"greater": 0
							}
						]
					},
					"roleDefinitionIds": [
						"/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c",
						"/providers/Microsoft.Authorization/roleDefinitions/b9331d33-8a36-4f8c-b097-4f54124fdb44"
					],
					"deployment": {
						"properties": {
							"mode": "incremental",
							"template": {
								"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
								"contentVersion": "1.0.0.0",
								"parameters": {
									"vmName": {
										"type": "string"
									},
									"location": {
										"type": "string"
									},
									"galleryApplication": {
										"type": "string"
									},
									"enableAutomaticUpgrade": {
										"type": "Bool"
									},
									"treatFailureAsDeploymentFailure": {
										"type": "Bool"
									},
									"configurationReference": {
										"type": "string"
									}
								},
								"resources": [
									{
										"apiVersion": "2023-03-01",
										"type": "Microsoft.Compute/virtualMachines/VMapplications",
										"name": "[concat(parameters('vmName'), '/', split(parameters('galleryApplication'), '/')[10])]",
										"location": "[parameters('location')]",
										"properties": {
											"packageReferenceId": "[parameters('galleryApplication')]",
											"enableAutomaticUpgrade": "[parameters('enableAutomaticUpgrade')]",
											"treatFailureAsDeploymentFailure": "[parameters('treatFailureAsDeploymentFailure')]",
											"configurationReference": ""
										}
									}
								]
							},
							"parameters": {
								"vmName": {
									"value": "[field('name')]"
								},
								"location": {
									"value": "[field('location')]"
								},
								"galleryApplication": {
									"value": "[parameters('galleryApplication')]"
								},
								"enableAutomaticUpgrade": {
									"value": "[parameters('enableAutomaticUpgrade')]"
								},
								"treatFailureAsDeploymentFailure": {
									"value": "[parameters('treatFailureAsDeploymentFailure')]"
								},
								"configurationReference": {
									"value": "[parameters('configurationReference')]"
								}
							}
						}
					}
				}
			}
		}
	},
	"type": "Microsoft.Authorization/policyDefinitions",
	"name": "25c202a4-16b4-403f-82d4-0dba3e3e689a"
}
Bylaw25c202a4-16b4-403f-82d4-0dba3e3e689a.bylaw.ps1 · 79 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '25c202a4-16b4-403f-82d4-0dba3e3e689a' {

    Properties @{
        DisplayName = 'Deploy Windows VM Application'
        Description = 'This policy will deploy a vm application to Windows virtual machines. You will need to assign the managed identity the reader role to the Azure Compute Gallery where the application resides. Users or service principles deploying virtual machines will also need the reader role assigned to the Azure Compute Gallery for the policy to take effect during new virtual machine deployments'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy.'
    }
    Parameter GalleryApplication @{
        Type        = 'String'
        DisplayName = 'VM Application Version'
        Description = 'The package reference id to vm application version to deploy'
        Metadata    = [ordered]@{ strongType = 'microsoft.compute/galleries/applications/versions'; portalReview = $true }
    }
    Parameter EnableAutomaticUpgrade @{
        Type        = 'Boolean'
        Default     = $true
        DisplayName = 'Enable Automatic Upgrade'
        Description = 'If set to true, when a new Gallery Application version is available it will be automatically updated'
        Metadata    = [ordered]@{ portalReview = $true }
    }
    Parameter TreatFailureAsDeploymentFailure @{
        Type        = 'Boolean'
        Default     = $false
        DisplayName = 'Treat Failure As Deployment Failure'
        Description = 'Mark application failure as VM deployment failure for failure handling while being installed/updated/removed'
        Metadata    = [ordered]@{ portalReview = $true }
    }
    Parameter ConfigurationReference @{
        Default     = ''
        DisplayName = 'Configuration File Reference'
        Description = 'Optional, Specifies the uri to an azure blob that will replace the default configuration for the package if provided'
        Metadata    = [ordered]@{ portalReview = $true }
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'storageProfile.osDisk.osType does not contain Windows' {
            Field storageProfile.osDisk.osType | Must -Not -ContainText 'Windows' -IfPresent
        }

        Require 'imageOffer does not contain Windows' {
            Field 'Microsoft.Compute/imageOffer' | Must -Not -ContainText 'Windows' -IfPresent
        }

        Require 'imagePublisher does not contain microsoftsqlserver' {
            Field 'Microsoft.Compute/imagePublisher' | Must -Not -ContainText 'microsoftsqlserver' -IfPresent
        }

        Require 'imagePublisher does not contain microsoftwindowsdesktop' {
            Field 'Microsoft.Compute/imagePublisher' | Must -Not -ContainText 'microsoftwindowsdesktop' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines' -Role 'Virtual Machine Contributor', 'Managed Application Publisher Operator' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'applicationProfile.galleryApplications and packageReferenceId are as required' {
            Count 'applicationProfile.galleryApplications[*]' -Where {
                Field packageReferenceId | Must -Be (Param GalleryApplication)
            } | Must -BeGreaterThan 0
        }
        Deployment -TemplateFile './templates/25c202a4-16b4-403f-82d4-0dba3e3e689a.json' -Parameters ([ordered]@{
                vmName                          = (Arm '[field(''name'')]')
                location                        = (Arm '[field(''location'')]')
                galleryApplication              = (Param GalleryApplication)
                enableAutomaticUpgrade          = (Param EnableAutomaticUpgrade)
                treatFailureAsDeploymentFailure = (Param TreatFailureAsDeploymentFailure)
                configurationReference          = (Param ConfigurationReference)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Compute/deploy-windows-vm-app/azurepolicy.json in Azure/Community-Policy, under the MIT licence.