Skip to content

Instantly share code, notes, and snippets.

@bmoore-msft
Last active July 16, 2020 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoore-msft/d578c815f319af7eb20d9d97df9bf657 to your computer and use it in GitHub Desktop.
Save bmoore-msft/d578c815f319af7eb20d9d97df9bf657 to your computer and use it in GitHub Desktop.
Deploy a template using a REST command from the az cli
<#
.Synopsis
This script will deploy an Azure Resource Manager Template using the "az rest" command to invoke the REST api directly
.Description
This script will deploy an Azure Resource Manager Template using the "az rest" command to invoke the REST api directly.
It deploys at resourceGroup scope but can be easily modified for any scope of deployment.
The current account context must already be selected before executing the script, use 'az account show' to show the context
#>
Param(
[string] [Parameter(Mandatory = $true)]$Location,
[string] [Parameter(Mandatory = $true)]$ResourceGroupName,
[string] [Parameter(Mandatory = $true)]$TemplateFile,
[string] $TemplateParametersFile,
[string] $DeploymentName = "rest-" + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm'),
[string] $mode = "incremental"
)
# get the management endpoint to invoke for the current context
$env = $(az cloud show -o json) | ConvertFrom-Json
$mgmtUri = $env.endpoints.resourceManager
# get the current subscriptionId for the resource uri
$ctx = $(az account show -o json) | ConvertFrom-Json
$subscriptionId = $ctx.id
$TemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateFile))
$TemplateObj = Get-Content $TemplateFile -Raw | ConvertFrom-JSON
if (![string]::IsNullOrWhiteSpace($TemplateParametersFile)) {
$TemplateParametersFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, $TemplateParametersFile))
$TemplateParametersObj = Get-Content $TemplateParametersFile -Raw | ConvertFrom-JSON
if (($TemplateParametersObj | Get-Member -Type NoteProperty 'parameters') -ne $null) {
$TemplateParametersObj = $TemplateParametersObj.parameters
}
}
else {
$TemplateParametersObj = @{ }
}
$body = @{
properties = @{
template = $TemplateObj
mode = $mode
parameters = $TemplateParametersObj
}
}
$bodyJSON = $body | ConvertTo-Json -Compress -Depth 30
$bodyJSON = $bodyJSON.Replace('"', '""') # add quotes in order to pass the command via pwsh.exe
$method = "PUT"
$uri = "$($mgmtUri)subscriptions/$($subscriptionId)/resourceGroups/$($resourceGroupName)/providers/Microsoft.Resources/deployments/$($deploymentName)?api-version=2019-10-01"
$(az rest --method $method --uri "$uri" --body "$BodyJSON" --verbose)
# wait for the deployment to reach a terminal state
do {
$status = $(az deployment group show -g "$resourceGroupName" -n "$deploymentName" -o json --verbose) | ConvertFrom-Json
Start-Sleep 5
} while ($status.properties.provisioningState -eq "Running")
$status | ConvertTo-Json -Depth 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment