Last active
July 16, 2020 19:13
-
-
Save bmoore-msft/d578c815f319af7eb20d9d97df9bf657 to your computer and use it in GitHub Desktop.
Deploy a template using a REST command from the az cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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