I often find that when building an ARM template, I need to test it multiple times. So, I created the script below that will create the resource group (is it doesn’t exist), run the test cmdlet (and stop if there is a problem), and deploy the template to Azure. It will create a new name for the deployment each time based on the file name, and the current time. This will allow you to view the details of each individual deployment in Azure.
$ResourceGroupName = ''
$TemplateFile = ''
$ParameterFile = ''
# Create the resource group, if not already there
try {
Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction Stop | Out-Null
} catch {
New-AzResourceGroup -Name $ResourceGroupName -Location "EastUS"
}
# Create parameter hashtable for splatting
$params = @{
ResourceGroupName = $ResourceGroupName
TemplateFile = $TemplateFile
TemplateParameterFile = $ParameterFile
}
# Test the template first, stop execution if test fails
$test = Test-AzResourceGroupDeployment @params
if($test){
$test
$test.Details.Details
break
}
$DeploymentName = [System.IO.Path]::GetFileNameWithoutExtension($TemplateFile) + '_' +
(Get-Date).ToString('yyyyMMddHHmm')
New-AzResourceGroupDeployment @params -Name $DeploymentName