Skip to content

ConvertTo-Bylaw

SYNOPSIS

Converts Azure Policy definition and initiative JSON into .bylaw.ps1 files that compile back to the same definition.

SYNTAX

ConvertTo-Bylaw [[-Path] <String[]>] [-InputObject <Object>] [-OutputPath <String>] [-Force] [-PassThru]
 [-AliasCachePath <String>] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

Reads policy definitions in the EPAC format or as plain JSON, writes a .bylaw.ps1 file for each, and proves each file before writing it: the file is compiled in the input's format and compared with the input by meaning (ADR-0039, ADR-0041). A file that doesn't prove equivalent isn't written.

Each result has a status: Converted, when the file uses only Bylaw's keywords; EscapeHatch, when some conditions are written as raw JSON with Condition (ADR-0040); or Failed, with diagnostics saying why. Require blocks are named from their condition, because the JSON has no descriptions: rename them to say what they require.

A definition that can't be converted is reported as a non-terminating error with its BYL6xxx code, so the others still convert; use -ErrorAction Stop to stop at the first. A definition without a name is named after its file, with a warning. Use -Verbose to list the files written. Each error and warning is shown as a block with a link to its page in the wiki, and a run ends with one line saying how many definitions converted. Both go to the host, out of the pipeline: -InformationAction Ignore hides them. A progress bar counts the definitions converted, then proved; $ProgressPreference = 'SilentlyContinue' hides it.

Every effect converts, with its details. A DeployIfNotExists template is written beside the file, as templates/\<name>.json, and returned in the result's Templates.

Initiatives convert too. In EPAC, a member referenced by policyDefinitionName becomes PolicyDefinition 'name', a policy in the build; a member referenced by ID keeps its ID, as the GUID alone for a built-in policy. A member too long for one line is splatted from a hashtable. In plain JSON, members from your own build keep their full IDs: to name them instead, set definitionScope (ADR-0035) and change them to PolicyDefinition 'name'.

EXAMPLES

EXAMPLE 1

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

EXAMPLE 2

ConvertTo-Bylaw -Path ./storage-tls.json | Select-Object Name, Status, Script

EXAMPLE 3

ConvertTo-Bylaw -Path ./Definitions -OutputPath ./policies -PassThru -ErrorAction SilentlyContinue |
    Where-Object Status -NE 'Converted' | Select-Object Name, Status, Diagnostics

Converts a folder, and lists the definitions that needed Condition or couldn't be converted, with why.

EXAMPLE 4

Get-ChildItem ./samples -Filter azurepolicy.json -Recurse | ConvertTo-Bylaw -OutputPath ./policies

Converts the files Get-ChildItem finds, as -Path would. Each file is named after its definition, so files with the same name in different folders don't overwrite each other.

EXAMPLE 5

Get-Content ./storage-tls.json -Raw | ConvertTo-Bylaw

Converts JSON from the pipeline. Without a file, a definition without a name can't be named after it, so it is reported.

PARAMETERS

-Path

Files and folders of definitions. Folders are searched for *.json and *.jsonc files. A file found there that is only part of a definition is skipped, since the full definition beside it is converted: a bare policy rule or parameters map, or a list of an initiative's members or groups, such as Community-Policy's azurepolicy.rules.json, azurepolicy.parameters.json, azurepolicyset.definitions.json and azurepolicyset.groups.json. So is an ARM deployment template, such as one ConvertTo-Bylaw writes to templates, and so is a file piped from Get-ChildItem, which is a search too. Use -Verbose to list the files skipped; a file named here is reported instead.

Type: String[]
Parameter Sets: (All)
Aliases:

Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-InputObject

A definition as JSON text or a parsed dictionary, from the pipeline. A file or folder, as Get-ChildItem outputs, is converted as if it were given to -Path.

Type: Object
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-OutputPath

The folder to write .bylaw.ps1 files to, mirroring the input folders. Without it, nothing is written and the results are returned. A relative path is relative to the current location. A folder that doesn't exist is created; one that is a file, or on a drive that doesn't exist, fails before anything is converted (BYL5041). When it is inside a folder given to -Path, its files aren't read as input. Two definitions that would write the same file are reported as BYL6011, and only the first is written.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Force

Overwrite existing .bylaw.ps1 files and templates. Without it, a definition whose file or template exists is left as it is.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-PassThru

Return the results when writing files too.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-AliasCachePath

The alias snapshot to check and shorten field names with. Defaults to the bundled one.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS

System.Management.Automation.PSObject

NOTES