💪Exploring the Azure Bicep PowerShell Module

Dave R - Microsoft Azure MVP☁️
CodeX
Published in
4 min readSep 25, 2021

Learn the features provided by Azure Bicep in PowerShell.

Exploring the Azure Bicep PowerShell Module

If you are familiar with PowerShell for automating the management of systems, build, test, and deploy solutions, there’s a PowerShell module you can leverage for Bicep and simplify the Bicep authoring experience.

In this article, we will explore some of the capabilities of the Bicep PowerShell module and work with a few examples.

Installation

You have two options to install the Bicep PowerShell module: you can try the stable or the pre-release version. The module is available through the PowerShell Gallery: https://www.powershellgallery.com/packages/Bicep/

To install the PowerShell module, you can use the cmdlet below:

Install-Module -Name Bicep

If you prefer to try the pre-release, use the cmdlet below:

Install-Module -Name Bicep -AllowPrerelease

*Depending on your environment, you might need to set the execution policy as unrestricted:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Cmdlets available in Bicep PowerShell Module.

Here’s a list of some of the cmdlets available in the Bicep PowerShell module:

Build Bicep.

This cmdlet is equivalent to the Bicep CLI command ‘ Bicep build’ and includes some additional features.

You can compile a single bicep file in your working directory:

Build-Bicep -Path storage.bicep

You can also specify the output directory:

Build-Bicep -Path 'c:\bicep\modules\storage.bicep' -OutputDirectory 'c:\armtemplates\storage.bicep'

Now think of a scenario in which you have multiple Bicep files. The cmdlet below allows you to compile all you Bicep files in a directory

Build-Bicep -Path 'c:\bicep\modules\'

You can also make exceptions, and you can compile all Bicep files in the working directory except one of them.

Build-Bicep -Path 'c:\bicep\modules\' -ExcludeFile storage.bicep

You can refer to the most recent documentation here.

ConvertTo-Bicep

This cmdlet will help you to decompile ARM templates into Bicep files. This is the equivalent of ‘bicep decompile’, and it comes with additional features.

To decompile a single ARM template file, you can use the cmdlet below:

ConvertTo-Bicep -Path storage.json

To decompile all the ARM templates in your working directory:

ConvertTo-Bicep -Path 'c:\armtemplates\'

The cmdlet ConverTo-Bicep allows the following parameters:

  • -Path
  • -OutputDirectory
  • -AsString
  • -Force

This cmdlet also supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

Get-BicepVersion

This cmdlet will help you validate the installed version and the latest available version of Bicep CLI.

Get-BicepVersion

Get-BicepApiReference

This cmdlet will help you get the ARM documentation in a browser for the provided resource type. For example, to get the latest ARM template reference for a resource type, use the cmdlet below:

Get-BicepApiReference -ResourceProvider Microsoft.Storage -Resource storageAccounts

Using the above cmdlet, you will be redirected to the ARM documentation for the ‘Microsoft.Storage’ resource provider, resource ‘storageAccounts’ using the latest API Version.

Install-BicepCLI and Uninstall-BicepCLI

These cmdlets will allow you to install a specific version of the Bicep CLI or uninstall it.

Install a specific Bicep CLI Version:

Install-BicepCLI -Version '0.2.328'

Uninstall Bicep CLI

Uninstall-BicepCLI -Force

Update-BicepCLI

This cmdlet allows you to get the latest version of the Bicep CLI release available.

Update-BicepCLI

Convert-JsonToBicep

This cmdlet helps you convert any valid JSON object to Bicep Language format.

In the following example, we convert a JSON array to Bicep Language:

$json = @'
[
{
"name": "wui-rule",
"properties": {
"description": "Allow WUI",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "8443",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
},
{
"name": "ssh-rule",
"properties": {
"description": "Allow SSH",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "22",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 101,
"direction": "Inbound"
}
},
{
"name": "ssl-rule",
"properties": {
"description": "Allow SSL",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "443",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 103,
"direction": "Inbound"
}
},
{
"name": "web-rule",
"properties": {
"description": "Allow WEB",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "80",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 104,
"direction": "Inbound"
}
}
]
'@
Convert-JsonToBicep -String $json

The image below shows the output from the above cmdlet.

Convert a JSON array to Bicep Language

New-BicepParameterFile

If you prefer to pass on values from a parameters file to a Bicep file, this will help you create an ARM Template parameter file based on your Bicep file.

PS C:\> Update-BicepParameterFile -Path .\storage.parameters.json

Update-BicepParameterFile

This cmdlet updates an existing ARM Template parameter file based on a Bicep file.

PS C:\> Update-BicepParameterFile -Path .\storage.parameters.json

Test-BicepFile

This cmdlet will help you validate a Bicep file. It returns true/false by default and can be made to produce JSON output.

PS C:\> Test-BicepFile -Path 'MyBicep.bicep'
Test-BicepFile

This cmdlet allows the following parameters:

  • -AcceptDiagnosticLevel
  • -IgnoreDiagnosticOutput
  • -OutputType
  • -Path

Hopefully, this gives you a better understanding of the capabilities of the Bicep PowerShell module.

Join the AzInsider email list here.

-Dave R.

--

--