Bylaw

Gallery › Azure Update Manager › Azure built-in policies

Configure periodic checking for missing system updates on azure Arc-enabled servers

Uses Condition Policy Modify Mode Indexed Version 2.3.0

Configure auto-assessment (every 24 hours) for OS updates on Azure Arc-enabled servers. You can control the scope of assignment according to machine subscription, resource group, location or tag. Learn more about this for Windows: https://aka.ms/computevm-windowspatchassessmentmode, for Linux: https://aka.ms/computevm-linuxpatchassessmentmode.

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONAzUpdateMgmtCenter_HCRP_AutoAssessmentMode_Modify.json · 163 lines
{
  "properties": {
    "displayName": "Configure periodic checking for missing system updates on azure Arc-enabled servers",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Configure auto-assessment (every 24 hours) for OS updates on Azure Arc-enabled servers. You can control the scope of assignment according to machine subscription, resource group, location or tag. Learn more about this for Windows: https://aka.ms/computevm-windowspatchassessmentmode, for Linux: https://aka.ms/computevm-linuxpatchassessmentmode.",
    "metadata": {
      "version": "2.3.0",
      "category": "Azure Update Manager"
    },
    "version": "2.3.0",
    "parameters": {
      "assessmentMode": {
        "type": "String",
        "metadata": {
          "displayName": "Assessment mode",
          "description": "Assessment mode for the machines."
        },
        "allowedValues": [
          "ImageDefault",
          "AutomaticByPlatform"
        ],
        "defaultValue": "AutomaticByPlatform"
      },
      "osType": {
        "type": "String",
        "metadata": {
          "displayName": "OS type",
          "description": "OS type for the machines."
        },
        "allowedValues": [
          "Windows",
          "Linux"
        ],
        "defaultValue": "Windows"
      },
      "locations": {
        "type": "Array",
        "metadata": {
          "displayName": "Machines locations",
          "description": "The list of locations from which machines need to be targeted.",
          "strongType": "location"
        },
        "defaultValue": []
      },
      "tagValues": {
        "type": "Object",
        "metadata": {
          "displayName": "Tags on machines",
          "description": "The list of tags that need to matched for getting target machines."
        },
        "defaultValue": {}
      },
      "tagOperator": {
        "type": "String",
        "metadata": {
          "displayName": "Tag operator",
          "description": "Matching condition for resource tags"
        },
        "allowedValues": [
          "All",
          "Any"
        ],
        "defaultValue": "Any"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.HybridCompute/machines"
          },
          {
            "value": "[requestContext().apiVersion]",
            "greaterOrEquals": "2022-03-10"
          },
          {
            "field": "Microsoft.HybridCompute/machines/osName",
            "equals": "[parameters('osType')]"
          },
          {
            "anyOf": [
              {
                "value": "[empty(parameters('locations'))]",
                "equals": true
              },
              {
                "field": "location",
                "in": "[parameters('locations')]"
              }
            ]
          },
          {
            "field": "[if(equals(tolower(parameters('osType')), 'windows'), 'Microsoft.HybridCompute/machines/osProfile.windowsConfiguration.patchSettings.assessmentMode', 'Microsoft.HybridCompute/machines/osProfile.linuxConfiguration.patchSettings.assessmentMode')]",
            "notEquals": "[parameters('assessmentMode')]"
          },
          {
            "anyOf": [
              {
                "value": "[empty(parameters('tagValues'))]",
                "equals": true
              },
              {
                "allOf": [
                  {
                    "value": "[parameters('tagOperator')]",
                    "equals": "Any"
                  },
                  {
                    "value": "[greaterOrEquals(if(empty(field('tags')), 0, length(intersection(parameters('tagValues'), field('tags')))), 1)]",
                    "equals": true
                  }
                ]
              },
              {
                "allOf": [
                  {
                    "value": "[parameters('tagOperator')]",
                    "equals": "All"
                  },
                  {
                    "value": "[equals(if(empty(field('tags')), 0, length(intersection(parameters('tagValues'), field('tags')))), length(parameters('tagValues')))]",
                    "equals": true
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "modify",
        "details": {
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/cd570a14-e51a-42ad-bac8-bafd67325302"
          ],
          "conflictEffect": "audit",
          "operations": [
            {
              "condition": "[equals(tolower(parameters('osType')), 'windows')]",
              "operation": "addOrReplace",
              "field": "Microsoft.HybridCompute/machines/osProfile.windowsConfiguration.patchSettings.assessmentMode",
              "value": "[parameters('assessmentMode')]"
            },
            {
              "condition": "[equals(tolower(parameters('osType')), 'linux')]",
              "operation": "addOrReplace",
              "field": "Microsoft.HybridCompute/machines/osProfile.linuxConfiguration.patchSettings.assessmentMode",
              "value": "[parameters('assessmentMode')]"
            }
          ]
        }
      }
    },
    "versions": [
      "2.3.0",
      "2.2.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/bfea026e-043f-4ff4-9d1b-bf301ca7ff46",
  "name": "bfea026e-043f-4ff4-9d1b-bf301ca7ff46"
}
Bylawbfea026e-043f-4ff4-9d1b-bf301ca7ff46.bylaw.ps1 · 80 lines
# Converted from AzUpdateMgmtCenter_HCRP_AutoAssessmentMode_Modify.json by ConvertTo-Bylaw.
Policy 'bfea026e-043f-4ff4-9d1b-bf301ca7ff46' {

    Properties @{
        DisplayName   = 'Configure periodic checking for missing system updates on azure Arc-enabled servers'
        Description   = 'Configure auto-assessment (every 24 hours) for OS updates on Azure Arc-enabled servers. You can control the scope of assignment according to machine subscription, resource group, location or tag. Learn more about this for Windows: https://aka.ms/computevm-windowspatchassessmentmode, for Linux: https://aka.ms/computevm-linuxpatchassessmentmode.'
        Version       = '2.3.0'
        Category      = 'Azure Update Manager'
        PolicyVersion = '2.3.0'
        Mode          = 'Indexed'
    }

    Parameter AssessmentMode @{
        AllowedValues = @('ImageDefault', 'AutomaticByPlatform')
        Default       = 'AutomaticByPlatform'
        DisplayName   = 'Assessment mode'
        Description   = 'Assessment mode for the machines.'
    }
    Parameter OsType @{
        AllowedValues = @('Windows', 'Linux')
        Default       = 'Windows'
        DisplayName   = 'OS type'
        Description   = 'OS type for the machines.'
    }
    Parameter Locations @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Machines locations'
        Description = 'The list of locations from which machines need to be targeted.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter TagValues @{
        Type        = 'Object'
        Default     = [ordered]@{  }
        DisplayName = 'Tags on machines'
        Description = 'The list of tags that need to matched for getting target machines.'
    }
    Parameter TagOperator @{
        AllowedValues = @('All', 'Any')
        Default       = 'Any'
        DisplayName   = 'Tag operator'
        Description   = 'Matching condition for resource tags'
    }

    Resource 'Microsoft.HybridCompute/machines' {

        When {
            Arm '[requestContext().apiVersion]' | Must -BeGreaterOrEqual '2022-03-10'
            Field osName | Must -Be (Param OsType)
            AnyOf {
                Arm '[empty(parameters(''locations''))]' | Must -Be $true
                Field location | Must -BeIn (Param Locations)
            }
            Condition '{ "field": "[if(equals(tolower(parameters(''osType'')), ''windows''), ''Microsoft.HybridCompute/machines/osProfile.windowsConfiguration.patchSettings.assessmentMode'', ''Microsoft.HybridCompute/machines/osProfile.linuxConfiguration.patchSettings.assessmentMode'')]", "notEquals": "[parameters(''assessmentMode'')]" }'
        }

        Require 'Requirement 1' {
            Arm '[empty(parameters(''tagValues''))]' | Must -Not -Be $true
        }

        Require 'tags are as required' {
            AnyOf {
                Param TagOperator | Must -Not -Be 'Any'
                Arm '[greaterOrEquals(if(empty(field(''tags'')), 0, length(intersection(parameters(''tagValues''), field(''tags'')))), 1)]' | Must -Not -Be $true
            }
        }

        Require 'tags are as required' {
            AnyOf {
                Param TagOperator | Must -Not -Be 'All'
                Arm '[equals(if(empty(field(''tags'')), 0, length(intersection(parameters(''tagValues''), field(''tags'')))), length(parameters(''tagValues'')))]' | Must -Not -Be $true
            }
        }
    }

    Modify -Role 'Azure Connected Machine Resource Administrator' -ConflictEffect Audit {
        Field osProfile.windowsConfiguration.patchSettings.assessmentMode | AddOrReplace (Param AssessmentMode) -Condition (Arm '[equals(tolower(parameters(''osType'')), ''windows'')]')
        Field osProfile.linuxConfiguration.patchSettings.assessmentMode | AddOrReplace (Param AssessmentMode) -Condition (Arm '[equals(tolower(parameters(''osType'')), ''linux'')]')
    }
}

Source: built-in-policies/policyDefinitions/Azure Update Manager/AzUpdateMgmtCenter_HCRP_AutoAssessmentMode_Modify.json in Azure/azure-policy, under the MIT licence.