r/PowerShell • • 13m ago

AI Disclosure Bylaw, a Pester-style PowerShell DSL that compiles to Azure Policy JSON (pre-release, looking for feedback)

If you've ever written Azure Policy by hand, you'll know the JSON gets painful fast: nested allOf/anyOf, long aliases, and conditions written the wrong way round (you describe what's non-compliant, not what's compliant).

There are good tools for deploying policy (Bicep, Terraform, EPAC), but I couldn't find much to help with authoring it. So I built Bylaw: a PowerShell DSL, loosely modelled on Pester, where you state what must be true and it compiles that into Azure Policy JSON.

An excerpt from one of the examples:

Require 'TLS 1.2 or later' {
    Field minimumTlsVersion | Must -Be 'TLS1_2'
}

What it does:

  • You describe the compliant state. Bylaw works out the condition that finds non-compliant resources, including negations and missing properties.
  • Short field names like minimumTlsVersion expand to full Azure aliases, and misspellings fail the build.
  • Build-Bylaw validates the whole definition (arrays, remediation, related resources, initiatives) and flags issues. It needs no Azure access and doesn't deploy anything.
  • Deterministic output, so diffs only show real changes.
  • ConvertTo-Bylaw turns existing policy JSON into Bylaw and only counts it a success if it compiles back to an identical policy.
  • It's just PowerShell, so you can use loops, data files and helper functions to generate policies.

I've run the conversion against all the Azure built-in, Azure Community and ALZ policies. The gallery shows them side by side, and the coverage page lists what it can't handle yet (including a few source policies with genuine bugs).

Install (PowerShell 7.2+):

Install-PSResource -Name Bylaw -Prerelease

In the interest of transparency: I built it with Claude Code, using an ADR-driven design process, and I reviewed and approved every design decision and PR.

It's pre-release, and what I'd find most useful is seeing how it copes with real-world custom policies. If you try ConvertTo-Bylaw on yours and it fails or produces something odd, I'd love an issue. Feedback on the syntax is welcome too.

1 Upvotes

0 comments sorted by