Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created July 15, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinGrote/97851bbf4cac963ac01d741683520b09 to your computer and use it in GitHub Desktop.
Save JustinGrote/97851bbf4cac963ac01d741683520b09 to your computer and use it in GitHub Desktop.
Use Bicep to define a template spec
#requires -version 7
function Set-AzBicepTemplateSpec {
[CmdletBinding()]
param(
#Template spec Resource ID to set
[Alias('Id')][Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]$ResourceId,
#Path to the bicep file
[Parameter(Mandatory)][String]$Path,
#Optional Path to the UI Definition
[String]$UIDefinitionPath = $($Path -replace '.bicep$','.ui.json'),
[Parameter(Mandatory)][String]$Version,
[String]$VersionDescription
)
begin {
$ErrorActionPreference = 'stop'
$bicepCmd = (Get-Command 'bicep' -ErrorAction SilentlyContinue) ?? (Write-Error 'bicep cli not found, ensure it is in your path')
}
process {
$tempPath = "TEMP:bicep-$(new-GUID).json"
$tempFile = New-Item $tempPath
try {
& $bicepCmd build $Path --outfile $tempFile
$templateFile = Get-Item $tempPath
$setTemplateSpecParams = @{
TemplateFile = $templateFile
ResourceId = $ResourceId
Version = $Version
}
if ($VersionDescription) {$setTemplateSpecParams.VersionDescription = $VersionDescription}
if ($UIDefinitionPath) {$setTemplateSpecParams.UIFormDefinitionFile = $UIDefinitionPath}
Set-AzTemplateSpec @setTemplateSpecParams
} catch {throw} finally {
Remove-Item $tempFile
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment