Created
April 5, 2018 04:59
-
-
Save dennisroche/30f586b4e2ff77b720a01b024b1d6f8f to your computer and use it in GitHub Desktop.
Validate Azure ARM template
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
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