Skip to content

Instantly share code, notes, and snippets.

@dennisroche
Created April 5, 2018 04:59
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 dennisroche/30f586b4e2ff77b720a01b024b1d6f8f to your computer and use it in GitHub Desktop.
Save dennisroche/30f586b4e2ff77b720a01b024b1d6f8f to your computer and use it in GitHub Desktop.
Validate Azure ARM template
function Format-ValidationOutput {
Param (
[Parameter(ValueFromPipeline=$true)] $ValidationOutput,
[int] $Depth = 0
)
return @($ValidationOutput | Where-Object { $_ -ne $null } | ForEach-Object { @(' ' * $Depth + ': ' + $_.Message) + @(Format-ValidationOutput @($_.Details) ($Depth + 1)) })
}
# Template
$template = $DeployAzureResourceGroupStep.Azure.ResourceGroupTemplate
Write-Verbose $template
$templateFile = New-TemporaryFile
Set-Content -Value $template -Path $templateFile
# Parameters (convert JSON to Hashtable)
$templateParameters = @{}
$tmp = $DeployAzureResourceGroupStep.Azure.ResourceGroupTemplateParameters | ConvertFrom-Json
$tmp.PSObject.Properties | ForEach-Object { $templateParameters[$_.Name] = $_.Value.value }
Write-Host "Template: $templateFile"
$templateParameters | Format-Table -AutoSize
Write-Host "Verifying ARM Template..." -NoNewline
$errorMessages = Test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile $templateFile -TemplateParameterObject $templateParameters
$errorMessages = $errorMessages | Format-ValidationOutput
if ($errorMessages) {
Write-Host " FAILED" -ForegroundColor Red
Throw "$name Azure Deployment Template is invalid with the following errors: $errorMessages"
}
Write-Host " VALID" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment