Skip to content

Instantly share code, notes, and snippets.

@bgelens
Created February 20, 2018 15:07
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 bgelens/35ab1c2fd1758574421362bf7dd632d1 to your computer and use it in GitHub Desktop.
Save bgelens/35ab1c2fd1758574421362bf7dd632d1 to your computer and use it in GitHub Desktop.
$armProfileModule = (Get-Module -Name AzureRM.profile -ListAvailable)[0]
Add-Type -Path "$($armProfileModule.ModuleBase)\Newtonsoft.Json.dll"
function Expand-AzureRMTemplate {
<#
.Synopsis
Validates ARM template in combination with parameter file and returns ARM expanded results.
.Example
Expand-AzureRMTemplate -TemplateFile c:\subnet.json -ParameterFile c:\param.json -ResourceGroupName 'myRG'
#>
[cmdletbinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string] $TemplateFile,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias('TemplateParameterFile')]
[string] $ParameterFile,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $ResourceGroupName
)
begin {
$currentContext = Get-AzureRmContext
$token = $currentContext.TokenCache.ReadItems() | ? {$_.tenantid -eq $currentContext.Tenant.Id}
}
process {
$templateParams = Get-Content -Raw $ParameterFile | ConvertFrom-Json
$body = @"
{
"properties": {
"template": $(Get-Content -Raw $TemplateFile),
"parameters": $($templateParams.parameters | ConvertTo-Json -Depth 100),
"mode": "Incremental"
}
}
"@
$iwrArgs = @{
Uri = "https://management.azure.com/subscriptions/$($currentContext.Subscription.Id)/resourcegroups/$ResourceGroupName/providers/Microsoft.Resources/deployments/$([guid]::NewGuid().guid)/validate?api-version=2017-05-10"
Headers = @{
Authorization = "Bearer $($token.AccessToken)"
'Content-Type' = 'application/json'
}
Method = 'Post'
Body = $body
UseBasicParsing = $true
}
$result = Invoke-WebRequest @iwrArgs
#pretty print
[Newtonsoft.Json.Linq.JObject]::Parse($result.Content).ToString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment