Skip to content

Building

Build-Bylaw [[-Path] <string[]>] [[-OutputPath] <string>] [-InitiativeOutputPath <string>]
            [-Format <Epac|PlainJson>] [-DefinitionScope <string>] [-PassThru]
            [-Settings <string>] [-AliasCachePath <string>] [-RoleCachePath <string>]
            [-AllowUnknownAlias] [-ThrottleLimit <int>] [-WhatIf]
  • Formats: PlainJson, the default, writes { "name", "properties" }, the layout Azure publishes built-in definitions in, for New-AzPolicyDefinition, the Azure CLI, Bicep, Terraform or the portal. It emits the policy as written: a literal effect stays literal, nothing is generated, and Category and Version are optional. Epac writes EPAC policy definition files with a $schema, and generates what EPAC requires: an effect parameter for a literal effect, and parameter display names. EPAC also needs Category and Version in Properties. The policy rule is the same in both, apart from the effect. -Format chooses for one build, and format in bylaw.json for every build.
  • Output layout: one file per policy or initiative, named after it, in a folder structure that mirrors the sources. Initiatives go to -InitiativeOutputPath when it is set. Files whose content hasn't changed aren't rewritten.
  • All or nothing: every file is compiled and every error reported. If anything fails, nothing is written.
  • No deletion: Bylaw never deletes output. It warns about JSON files in the output folders that the build didn't produce, so you can remove policies and initiatives you've deleted. Deployment template files are inputs, so they're never reported.
  • Isolation: each file is compiled in a fresh runspace, so variables and functions can't leak between files. Several files compile at once; -ThrottleLimit sets how many, and -ThrottleLimit 1 compiles one at a time. Files that change environment variables or other process-wide state should be built with -ThrottleLimit 1.
  • Unique names: policy and initiative names must be unique across a build, ignoring case.
  • -PassThru returns a result for each policy and initiative, with its Kind, Name, source, output file, JSON and warnings.

Aliases

Bylaw checks field names against an alias snapshot, offline. The bundled snapshot has every resource type's aliases as of its generation date, so most policies need nothing more. For types or aliases Azure has added since, refresh it from Azure. This needs Az.Resources and a signed-in account, and only reads alias metadata:

Connect-AzAccount
Update-BylawAliasCache

That writes ~/.bylaw/aliases.json, which Bylaw then uses by default. To limit it to some providers, use -Namespace 'Microsoft.Storage', 'Microsoft.KeyVault'. To keep a snapshot with your policies, write it with -OutputPath and point bylaw.json at it.

The snapshot used is the first of: -AliasCachePath, aliasCache in bylaw.json, ~/.bylaw/aliases.json, the bundled snapshot.

Roles

Modify -Role and DeployIfNotExists -Role take built-in role names, such as 'Security Admin', which Bylaw turns into role definition IDs using a role map, offline. The bundled map has every built-in role as of its generation date. For roles Azure has added since, refresh it from Azure, which only reads role definitions:

Update-BylawRoleCache

That writes ~/.bylaw/roles.json. Built-in role IDs are the same in every tenant, so the map can be shared. Custom roles aren't in it; give them by GUID or full role definition ID.

The map used is the first of: -RoleCachePath, roleCache in bylaw.json, ~/.bylaw/roles.json, the bundled map.

bylaw.json

An optional settings file. Bylaw looks for it from -Path upwards, stopping at the repository root, or uses -Settings.

{
  "languageVersion": "1.0",
  "format": "Epac",
  "aliasCache": "./aliases.json",
  "roleCache": "./roles.json",
  "initiativeOutputPath": "./Definitions/policySetDefinitions",
  "definitionScope": "/providers/Microsoft.Management/managementGroups/contoso"
}

languageVersion pins the language's behaviour, so upgrading Bylaw never changes the output of existing policies by surprise. It defaults to 1.0. format is PlainJson, the default, or Epac, for every build that uses the file. Paths are relative to bylaw.json. definitionScope is a management group or subscription ID. Command-line parameters take precedence over these settings, and unknown keys fail the build.