You may be familiar with Microsoft’s Azure Quickstart Templates in GitHub: https://github.com/Azure/azure-quickstart-templates
I was looking for a way to run these in a dynamic fashion, without hard coding any values. This was the plan:
1. Using GitHub's REST API, pull list of ARM templates available from https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts
2. Open PowerShell Gridview to display all enumerated templates and allow for a JSON template to be selected
3. Pull a list of parameters from the chosen JSON template
4. Prompt in shell for the values for those parameters and store those into a hashtable
5. Finally, execute the deployment with the template & hashtable containing the parameters
Usage:
- Copy and paste the script shown below into a PowerShell editor such as VSCode, PowerShell ISE, etc.
- Run it.
- A GridView will pop up.
- Select a top level template.
- Continue selecting through recursively until you reach a path that contains an azuredeploy.json file.
- Select the azuredeploy.json and click OK in the GridView window.
- Provide values for the parameters:
- Note that the script looks for the word ‘password’ in the property name, and if so, stores that particular input as a SecureString in the hashtable.
- Note that the script will show you the Default value from the JSON parameters. If in doubt, simply copy and paste it into the input field, as seen below:
- A look at a completed Hashtable containing values for all the parameters:
Name Value
---- -----
location resourceGroup().location
ubuntuOSVersion Ubuntu-2004
virtualNetworkName vNet
dnsLabelPrefix testing
authenticationType password
vmSize Standard_D2s_v3
adminPasswordOrKey System.Security.SecureString
networkSecurityGroupName SecGroupNet
adminUsername administrator
subnetName
securityType TrustedLaunch
vmName simpleLinuxVM
- You will now be prompted for a Resource Group to be used as a target for the deployment. If you have multiple Subscriptions, remember to use Set-AzContext to focus on the targeted Subscription.
- Finally, the template and the parameters provided will be sent to the New-AzResourceGroupDeployment for deployment.
PowerShell script:
.SYNOPSIS
Dynamically run Azure Quickstart Templates using PowerShell and GitHub REST API.
.NOTES
Author: Randy@HumbleSystems.online
Date:
20230802 v1
#>
# .1 Provide root value to start with
$TreeUI = "https://api.github.com/repos/Azure/azure-quickstart-templates/contents/quickstarts?ref=master"
# .2 Execute and store results from URI
$RestResult = Invoke-RestMethod -Uri $TreeUI
# .3 Dynamically go through the folders until an azuredeploy.json file is selected:
$JsonURI = $null
while ($JsonURI.Name -ne "azuredeploy.json") {
$JsonURI = $RestResult | Out-GridView -PassThru -Title "Continue to select items until an azuredeploy.json file is selected."
$RestResult = Invoke-RestMethod -Uri $JsonURI.Url
}
# Download azuredeploy.json to grab parameters, which will show all possible parameters and not just mandatory ones.
$JSONParamsFromURI = Invoke-RestMethod -Method Get -Uri $JsonURI.download_url
# Optionally, use the parameters from azuredeploy.parameters.json. Only the mandatory required fields are prompted for, as opposed to azuredeploy.json - which contains all possible parameters.
# $JsonParamsURI = $JsonURI.download_url -replace "azuredeploy.json","azuredeploy.parameters.json"
# $JSONParamsFromURI = Invoke-RestMethod -Method Get -Uri $JsonParamsURI
# Ask for and store values in a hashtable dynamically based on NoteProperties from the json.
$ParamsHash = New-Object HashTable
$JSONParamsFromURI.parameters | Get-Member -MemberType NoteProperty | foreach {
if ($_.Name -like "*password*") {
$ParamsHash.Add(`
$($_.Name),`
(`
ConvertTo-SecureString -AsPlainText -Force `
(Read-Host -Prompt "`nProvide value (converted to SecureString) for $($_.Name).`nDescription: $($JSONParamsFromURI.parameters.$($_.Name).metadata.Description)`nDefault: $($JSONParamsFromURI.parameters.$($_.Name).defaultValue)`n")) `
)
}
else {
$ParamsHash.Add($($_.Name), (Read-Host -Prompt "`nProvide value for $($_.Name).`nDescription: $($JSONParamsFromURI.parameters.$($_.Name).metadata.Description)`nDefault: $($JSONParamsFromURI.parameters.$($_.Name).defaultValue)`n"))
}
}
# 3 AND, Deploy it
$RGName = (Get-AzResourceGroup | Out-GridView -PassThru -Title "Select target resource group for deployment").ResourceGroupName
New-AzResourceGroupDeployment -ResourceGroupName $RGName -TemplateUri $JsonURI.download_url -TemplateParameterObject $ParamsHash #-WhatIf
Leave a Reply