Skip to content

Converting existing policies

ConvertTo-Bylaw turns existing policy definitions and initiatives into .bylaw.ps1 files. It reads EPAC definitions, plain { name, properties } JSON, or a properties object on its own, from .json and .jsonc files, folders of them, or the pipeline:

ConvertTo-Bylaw -Path ./Definitions -OutputPath ./policies

Every file is proven before it is written: it is compiled in the input's format (EPAC when the input has a $schema, plain JSON otherwise) and compared with the input by meaning, not text (ADR-0041). A file that can't be proven equivalent isn't written. For example, storage-deny-public-network-access.json converts to:

# Converted from storage-deny-public-network-access.json by ConvertTo-Bylaw.
Policy 'storage-deny-public-network-access' {

    Properties @{
        DisplayName = 'Storage accounts should disable public network access'
        Description = 'Denies storage accounts that allow public network access or allow-all IP rules.'
        Version     = '1.0.0'
        Category    = 'Storage'
        Mode        = 'All'
    }

    Resource 'Microsoft.Storage/storageAccounts' {

        When {
            Field kind | Must -BeIn @('StorageV2', 'BlockBlobStorage')
        }

        Require 'publicNetworkAccess is Disabled' {
            Field publicNetworkAccess | Must -Be 'Disabled'
        }

        Require 'networkAcls.ipRules and value are as required' {
            Each 'networkAcls.ipRules[*]' {
                Field value | Must -Not -Be '0.0.0.0/0'
            }
        }
    }

    Effect Deny
}
  • The violation becomes Require blocks. The conditions before it are When, and each alternative of the violation is a Require, inverted back into the compliant state, with the missing-field handling recognised so -IfPresent and -Strict come back where they were. JSON has no descriptions, so a Require is named after what it tests: rename them to say what they require.
  • Effects come back as their keywords, with their details: Modify, Append, DenyAction, Manual, AuditIfNotExists and DeployIfNotExists, with roles by name where the role map has them. EPAC's generated effect parameter becomes the literal effect, as above, since the EPAC build generates it again. A DeployIfNotExists template is written beside the file, as templates/<name>.json.
  • Initiatives become Initiative files. EPAC members referenced by name are written by name, built-in policies by GUID, and any other ID in full.
  • What Bylaw's keywords can't express is written with Condition (Raw conditions), and the result's status is EscapeHatch rather than Converted, with a warning (BYL6005). Check those statements, and replace them as the language grows.
  • What it can't convert is reported as an error, with a BYL6xxx code and the reason, and nothing is written for it; the other definitions still convert. Use -ErrorAction Stop to stop at the first.
  • Names: a definition without a name, such as a properties file, is named after its file, with a warning (BYL6008).
  • Mode: a policy without a mode is converted as Mode = 'Indexed', which is what Azure treats a missing mode as, with a warning (BYL6012). New-AzPolicyDefinition is the exception: it deploys a definition without a mode as All. Fields Azure sets, policyType and versions, are ignored.
  • Build warnings: when a converted file compiles with a warning, such as a location check under Mode = 'All', conversion reports it as BYL6009, with the warning's own code and line, so Build-Bylaw holds no surprises.
  • Aliases: fields are shortened against the alias snapshot where they resolve back. A type or alias the snapshot lacks is written in full, and the file then builds only with -AllowUnknownAlias (BYL6007): run Update-BylawAliasCache first for short names everywhere.
  • Existing files are left as they are, with a warning (BYL6006), unless -Force is given. -WhatIf writes nothing, -Verbose lists the files written, and without -OutputPath, or with -PassThru, each result is returned: Name, Source, Status, Script, Templates, OutputFile and Diagnostics.

Coverage

The test suite converts a pinned copy of the Azure Landing Zones Library, at 3ac1ed6 (2026-11-21), and fails if any definition converts worse than its recorded baseline (ADR-0042). Every conversion is proven:

  • 149 of 149 policy definitions converted (1 with Condition)
  • 44 of 44 initiatives converted

Condition holds what Bylaw's keywords can't express yet. Every conversion is committed in tests/Alz/converted, so you can read what real ALZ policies look like in Bylaw. ./build/alz/Get-AlzCoverage.ps1 writes the full report to out/alz-coverage.md.