Bylaw

Gallery › Guest Configuration › Azure built-in policies

Audit Windows machines that do not have the specified Windows PowerShell modules installed

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 3.0.0

Requires that prerequisites are deployed to the policy assignment scope. For details, visit https://aka.ms/gcpol. Machines are non-compliant if a module isn't available in a location specified by the environment variable PSModulePath.

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

Azure Policy JSONWindowsPowerShellModules_AINE.json · 282 lines
{
  "properties": {
    "displayName": "Audit Windows machines that do not have the specified Windows PowerShell modules installed",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Requires that prerequisites are deployed to the policy assignment scope. For details, visit https://aka.ms/gcpol. Machines are non-compliant if a module isn't available in a location specified by the environment variable PSModulePath.",
    "metadata": {
      "category": "Guest Configuration",
      "version": "3.0.0",
      "requiredProviders": [
        "Microsoft.GuestConfiguration"
      ],
      "guestConfiguration": {
        "name": "WindowsPowerShellModules",
        "version": "1.*",
        "configurationParameter": {
          "Modules": "[PowerShellModules]PowerShellModules1;Modules"
        }
      }
    },
    "version": "3.0.0",
    "parameters": {
      "IncludeArcMachines": {
        "type": "string",
        "metadata": {
          "displayName": "Include Arc connected servers",
          "description": "By selecting this option, you agree to be charged monthly per Arc connected machine.",
          "portalReview": "true"
        },
        "allowedValues": [
          "true",
          "false"
        ],
        "defaultValue": "false"
      },
      "Modules": {
        "type": "string",
        "metadata": {
          "displayName": "PowerShell Modules",
          "description": "A semicolon-separated list of the names of the PowerShell modules that should be installed. You may also specify a specific version of a module that should be installed by including a comma after the module name, followed by the desired version. Example: PSDscResources; SqlServerDsc, 12.0.0.0; ComputerManagementDsc, 6.1.0.0"
        }
      },
      "effect": {
        "type": "string",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of this policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "anyOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Compute/virtualMachines"
              },
              {
                "anyOf": [
                  {
                    "field": "Microsoft.Compute/imagePublisher",
                    "in": [
                      "esri",
                      "incredibuild",
                      "MicrosoftDynamicsAX",
                      "MicrosoftSharepoint",
                      "MicrosoftVisualStudio",
                      "MicrosoftWindowsDesktop",
                      "MicrosoftWindowsServerHPCPack"
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "MicrosoftWindowsServer"
                      },
                      {
                        "field": "Microsoft.Compute/imageSKU",
                        "notLike": "2008*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "MicrosoftSQLServer"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "notLike": "SQL2008*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "microsoft-dsvm"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "dsvm-win*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "microsoft-ads"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "in": [
                          "standard-data-science-vm",
                          "windows-data-science-vm"
                        ]
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "batch"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "equals": "rendering-windows2016"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "center-for-internet-security-inc"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "cis-windows-server-201*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "pivotal"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "bosh-windows-server*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Compute/imagePublisher",
                        "equals": "cloud-infrastructure-services"
                      },
                      {
                        "field": "Microsoft.Compute/imageOffer",
                        "like": "ad*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Compute/virtualMachines/osProfile.windowsConfiguration",
                            "exists": "true"
                          },
                          {
                            "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
                            "like": "Windows*"
                          }
                        ]
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Compute/imageSKU",
                            "exists": "false"
                          },
                          {
                            "allOf": [
                              {
                                "field": "Microsoft.Compute/imageSKU",
                                "notLike": "2008*"
                              },
                              {
                                "field": "Microsoft.Compute/imageOffer",
                                "notLike": "SQL2008*"
                              }
                            ]
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "value": "[parameters('IncludeArcMachines')]",
                "equals": "true"
              },
              {
                "anyOf": [
                  {
                    "allOf": [
                      {
                        "field": "type",
                        "equals": "Microsoft.HybridCompute/machines"
                      },
                      {
                        "field": "Microsoft.HybridCompute/imageOffer",
                        "like": "windows*"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "type",
                        "equals": "Microsoft.ConnectedVMwarevSphere/virtualMachines"
                      },
                      {
                        "field": "Microsoft.ConnectedVMwarevSphere/virtualMachines/osProfile.osType",
                        "like": "windows*"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.GuestConfiguration/guestConfigurationAssignments",
          "name": "[concat('WindowsPowerShellModules$pid', uniqueString(policy().assignmentId, policy().definitionReferenceId))]",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.GuestConfiguration/guestConfigurationAssignments/complianceStatus",
                "equals": "Compliant"
              },
              {
                "field": "Microsoft.GuestConfiguration/guestConfigurationAssignments/parameterHash",
                "equals": "[base64(concat('[PowerShellModules]PowerShellModules1;Modules', '=', parameters('Modules')))]"
              }
            ]
          }
        }
      }
    },
    "versions": [
      "3.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/3e4e2bd5-15a2-4628-b3e1-58977e9793f3",
  "name": "3e4e2bd5-15a2-4628-b3e1-58977e9793f3"
}
Bylaw3e4e2bd5-15a2-4628-b3e1-58977e9793f3.bylaw.ps1 · 116 lines
# Converted from WindowsPowerShellModules_AINE.json by ConvertTo-Bylaw.
Policy '3e4e2bd5-15a2-4628-b3e1-58977e9793f3' {

    Properties @{
        DisplayName   = 'Audit Windows machines that do not have the specified Windows PowerShell modules installed'
        Description   = 'Requires that prerequisites are deployed to the policy assignment scope. For details, visit https://aka.ms/gcpol. Machines are non-compliant if a module isn''t available in a location specified by the environment variable PSModulePath.'
        Category      = 'Guest Configuration'
        Version       = '3.0.0'
        PolicyVersion = '3.0.0'
        Mode          = 'Indexed'
        Metadata      = [ordered]@{ requiredProviders = @('Microsoft.GuestConfiguration'); guestConfiguration = [ordered]@{ name = 'WindowsPowerShellModules'; version = '1.*'; configurationParameter = [ordered]@{ Modules = '[PowerShellModules]PowerShellModules1;Modules' } } }
    }

    Parameter IncludeArcMachines @{
        Type          = 'string'
        AllowedValues = @('true', 'false')
        Default       = 'false'
        DisplayName   = 'Include Arc connected servers'
        Description   = 'By selecting this option, you agree to be charged monthly per Arc connected machine.'
        JsonName      = 'IncludeArcMachines'
        Metadata      = [ordered]@{ portalReview = 'true' }
    }
    Parameter Modules @{
        Type        = 'string'
        DisplayName = 'PowerShell Modules'
        Description = 'A semicolon-separated list of the names of the PowerShell modules that should be installed. You may also specify a specific version of a module that should be installed by including a comma after the module name, followed by the desired version. Example: PSDscResources; SqlServerDsc, 12.0.0.0; ComputerManagementDsc, 6.1.0.0'
        JsonName    = 'Modules'
    }
    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of this policy'
    }

    Resource * {

        Require 'type, imagePublisher, imageSKU and 3 more are as required' {
            AnyOf {
                Field type | Must -Not -Be 'Microsoft.Compute/virtualMachines' -IfPresent
                AllOf {
                    Field 'Microsoft.Compute/imagePublisher' | Must -Not -BeIn @('esri', 'incredibuild', 'MicrosoftDynamicsAX', 'MicrosoftSharepoint', 'MicrosoftVisualStudio', 'MicrosoftWindowsDesktop', 'MicrosoftWindowsServerHPCPack') -IfPresent
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftWindowsServer' -IfPresent
                        Field 'Microsoft.Compute/imageSKU' | Must -BeLike '2008*'
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'MicrosoftSQLServer' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -BeLike 'SQL2008*'
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'microsoft-dsvm' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'dsvm-win*' -IfPresent
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'microsoft-ads' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -BeIn @('standard-data-science-vm', 'windows-data-science-vm') -IfPresent
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'batch' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -Be 'rendering-windows2016' -IfPresent
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'center-for-internet-security-inc' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'cis-windows-server-201*' -IfPresent
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'pivotal' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'bosh-windows-server*' -IfPresent
                    }
                    AnyOf {
                        Field 'Microsoft.Compute/imagePublisher' | Must -Not -Be 'cloud-infrastructure-services' -IfPresent
                        Field 'Microsoft.Compute/imageOffer' | Must -Not -BeLike 'ad*' -IfPresent
                    }
                    AnyOf {
                        AllOf {
                            Field 'Microsoft.Compute/virtualMachines/osProfile.windowsConfiguration' | Must -Not -Exist
                            Field 'Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType' | Must -Not -BeLike 'Windows*' -IfPresent
                        }
                        AllOf {
                            Field 'Microsoft.Compute/imageSKU' | Must -Exist
                            AnyOf {
                                Field 'Microsoft.Compute/imageSKU' | Must -BeLike '2008*'
                                Field 'Microsoft.Compute/imageOffer' | Must -BeLike 'SQL2008*'
                            }
                        }
                    }
                }
            }
        }

        Require 'type, imageOffer and osProfile.osType are as required' {
            AnyOf {
                Param IncludeArcMachines | Must -Not -Be 'true'
                AllOf {
                    AnyOf {
                        Field type | Must -Not -Be 'Microsoft.HybridCompute/machines' -IfPresent
                        Field 'Microsoft.HybridCompute/imageOffer' | Must -Not -BeLike 'windows*' -IfPresent
                    }
                    AnyOf {
                        Field type | Must -Not -Be 'Microsoft.ConnectedVMwarevSphere/virtualMachines' -IfPresent
                        Field 'Microsoft.ConnectedVMwarevSphere/virtualMachines/osProfile.osType' | Must -Not -BeLike 'windows*' -IfPresent
                    }
                }
            }
        }
    }

    AuditIfNotExists 'Microsoft.GuestConfiguration/guestConfigurationAssignments' -Name (Arm '[concat(''WindowsPowerShellModules$pid'', uniqueString(policy().assignmentId, policy().definitionReferenceId))]') -Effect (Param Effect) {
        Require 'complianceStatus and parameterHash are as required' {
            Field complianceStatus | Must -Be 'Compliant'
            Field parameterHash | Must -Be (Arm '[base64(concat(''[PowerShellModules]PowerShellModules1;Modules'', ''='', parameters(''Modules'')))]')
        }
    }
}

Source: built-in-policies/policyDefinitions/Guest Configuration/WindowsPowerShellModules_AINE.json in Azure/azure-policy, under the MIT licence.