Last active
July 19, 2024 14:30
-
-
Save rbleattler/2e21aea3e82e082b13574c8353536d85 to your computer and use it in GitHub Desktop.
Returns a list of Parameters, Variables, and Outputs from an Arm template in ~yamlish format. Prepends each line with a prefix for pasting in comments. Prefix is configurable
This file contains 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
#Requires -Modules "powershell-yaml" | |
function Get-ArmParamsVarsOutputs { | |
[CmdletBinding()] | |
param( | |
[string] $ArmTemplatePath, | |
[ValidateSet('parameters', 'variables', 'outputs')] | |
[string[]] $OutputTypes = @('parameters', 'variables', 'outputs'), | |
[string] $Prefix = '//', | |
[switch] $NoPrefix | |
) | |
begin { | |
function Get-CleanParameters { | |
param( | |
[System.Collections.Hashtable] | |
$parameters | |
) | |
if ($parameters.Count -eq 0) { | |
Write-Warning "No parameters found in the ARM template" | |
} | |
$outParams = $parameters.Clone() | |
$parameters.Keys.ForEach({ | |
Write-Debug "Processing parameter : $_" | |
$keyCount = $outParams[$_].Keys.Count | |
Write-Debug "Key Count : $keyCount" | |
$containsDefault = $outParams[$_].Keys.Contains('defaultValue') -and ![string]::IsNullOrEmpty($outParams[$_].defaultValue) | |
$containsAllowedValues = $outParams[$_].Keys.Contains('allowedValues') -and ![string]::IsNullOrEmpty($outParams[$_].allowedValues) | |
Write-Debug "Contains Default : $containsDefault" | |
Write-Debug "Contains Allowed Values : $containsAllowedValues" | |
if ($keyCount -gt 0 -and $containsDefault -or $containsAllowedValues) { | |
$selectProperties = @() | |
if (![string]::IsNullOrWhiteSpace($outParams[$_].defaultValue)) { | |
$selectProperties += 'defaultValue' | |
} | |
if (![string]::IsNullOrWhiteSpace($outParams[$_].allowedValues)) { | |
$selectProperties += 'allowedValues' | |
} | |
$outParams[$_] = $parameters[$_] | Select-Object -Property $selectProperties | |
} else { | |
$outParams[$_] = "" | |
} | |
}) | |
return $outParams | |
} | |
# function Get-OutputYaml { | |
# param( | |
# [hashtable] $InputData | |
# ) | |
# $InputData | ConvertTo-Yaml -KeepArray | ForEach-Object { | |
# '{0} {1}' -f $Prefix, $_ | |
# } | |
# } | |
function Get-OutputYaml($headingName, $InputData) { | |
"$Prefix $HeadingName (from Arm) :`n$Prefix" | |
$rawYaml = (ConvertTo-Yaml -Data $InputData).Split("`n") | |
Write-Debug "[Get-OutputYaml] Raw Yaml Line Count : $($rawYaml.Count)" | |
$rawYaml.ForEach({ | |
'{0} {1}' -f $Prefix, $PSItem | |
}) | |
} | |
$raw = Get-Content $armTemplatePath -Raw | ConvertFrom-Json -Depth 20 -AsHashtable | |
switch ($outputTypes) { | |
'parameters' { | |
$parameters = Get-CleanParameters -parameters $raw.parameters | |
} | |
'variables' { | |
$variables = $raw.variables | |
} | |
'outputs' { | |
$outputs = $raw.outputs | |
} | |
default { | |
$parameters = $parameters = Get-CleanParameters -parameters $raw.parameters | |
$variables = $raw.variables | |
$outputs = $raw.outputs | |
} | |
} | |
if ($NoPrefix) { | |
$Prefix = '' | |
} | |
} | |
process { | |
switch ($outputTypes) { | |
'parameters' { | |
Get-OutPutYaml -headingName "Parameters" -input $parameters | |
} | |
'variables' { | |
Get-OutPutYaml -headingName "Variables" -input $variables | |
} | |
'outputs' { | |
Get-OutPutYaml -headingName "Outputs" -input $outputs | |
} | |
default { | |
Get-OutPutYaml -headingName "Parameters" -input $parameters | |
} | |
} | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment