Skip to content

Build-Bylaw

SYNOPSIS

Compiles *.bylaw.ps1 files to Azure Policy definition files.

SYNTAX

Build-Bylaw [[-Path] <String[]>] [[-OutputPath] <String>] [-InitiativeOutputPath <String>]
 [-DefinitionScope <String>] [-Format <String>] [-PassThru] [-Settings <String>] [-AliasCachePath <String>]
 [-RoleCachePath <String>] [-AllowUnknownAlias] [-ThrottleLimit <Int32>]
 [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

Finds *.bylaw.ps1 files under -Path (recursively), compiles each one in a fresh runspace, and writes one file per policy to -OutputPath, mirroring the source folder structure. Initiatives are written the same way, to -InitiativeOutputPath (ADR-0035).

Every file is compiled and every error is reported before the build fails. If anything fails, no files are written.

Each error and warning is shown as a block: where it is and its code, then the message and a link to its page in the wiki. A build ends with one line saying what it built, where, and how many files it wrote. Both go to the host, out of the pipeline: -InformationAction Ignore hides them. The error itself, and -PassThru's results, are for scripts. While it compiles, a progress bar counts the files done; $ProgressPreference = 'SilentlyContinue' hides it.

Compiling a policy file runs it: .bylaw.ps1 files are PowerShell scripts and run with your permissions. Don't build files you don't trust.

EXAMPLES

EXAMPLE 1

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

EXAMPLE 2

Build-Bylaw -Path ./policies/storage.bylaw.ps1 -PassThru | Select-Object Name, Json

EXAMPLE 3

Get-ChildItem ./policies -Filter *.bylaw.ps1 -Recurse | Build-Bylaw -OutputPath ./out

Builds the files Get-ChildItem finds. Piped files are compiled together, as one build, so names are checked for clashes across all of them and initiatives can include their policies.

EXAMPLE 4

Build-Bylaw ./policies -OutputPath ./Definitions/policyDefinitions -InitiativeOutputPath ./Definitions/policySetDefinitions -Format Epac

Builds EPAC files into an EPAC project's folders. To use EPAC for every build, set "format": "Epac" in bylaw.json instead.

PARAMETERS

-Path

Source files or folders. Defaults to the current folder.

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

Required: False
Position: 1
Default value: .
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False

-OutputPath

Output folder. Required unless -PassThru is used. 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 the build before anything is compiled (BYL5041).

Type: String
Parameter Sets: (All)
Aliases:

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

-InitiativeOutputPath

Folder for initiatives. Defaults to initiativeOutputPath in bylaw.json, then to -OutputPath. For EPAC, use its policySetDefinitions folder (ADR-0035).

Type: String
Parameter Sets: (All)
Aliases:

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

-DefinitionScope

The scope the build's policies are deployed at, such as /providers/Microsoft.Management/managementGroups/contoso. Plain JSON initiatives need it to give their members from the build a full policyDefinitionId (ADR-0035). Defaults to definitionScope in bylaw.json.

Type: String
Parameter Sets: (All)
Aliases:

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

-Format

Output format. Defaults to format in bylaw.json, then to PlainJson (ADR-0011): PlainJson writes { name, properties } definitions, as Azure publishes built-in ones, for New-AzPolicyDefinition, the Azure CLI, Bicep or Terraform (ADR-0027). The policy is emitted as written: a literal effect stays literal and nothing is generated. Epac writes EPAC policy definition files, with a $schema, and generates what EPAC requires, such as an effect parameter (ADR-0023). Set "format": "Epac" in bylaw.json to use it for every build of an EPAC project.

Type: String
Parameter Sets: (All)
Aliases:

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

-PassThru

Returns one result per policy and initiative: Kind, Name, SourceFile, SourceLine, OutputFile, Json, Warnings. Without -OutputPath, compiles in memory and writes nothing.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

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

-Settings

Path to a bylaw.json settings file. By default, the nearest bylaw.json at or above -Path is used, up to the repository root.

Type: String
Parameter Sets: (All)
Aliases:

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

-AliasCachePath

Alias snapshot to resolve and validate aliases against, instead of the default.

Type: String
Parameter Sets: (All)
Aliases:

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

-RoleCachePath

Role map to resolve Modify -Role names against, instead of the default.

Type: String
Parameter Sets: (All)
Aliases:

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

-AllowUnknownAlias

Reports unknown aliases as warnings instead of errors, for the whole build.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

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

-ThrottleLimit

How many files to compile at once, each in its own fresh runspace. Defaults to the number of processors, at most 16. Use 1 to compile one file at a time. Output is the same either way.

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: 0
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

Bylaw.BuildResult

NOTES