I’m building an Azure DevOps pipeline, and wanted to separate out parameter input into it’s own file which may be re-used across multiple different task types. I investigated a method of declaring parameters in JSON, and then ingesting them in a PowerShell script and using it to populate PowerShell variables.
For the time being, I’ve simplified my code to just use a direct PowerShell dot-sourced file, but want to keep this code snippet here for future reference.
JSON File
{
"subscriptionid": "3d22393a",
"resourceGroupName": "test-rg",
"azureLocation": "EastUS2",
"automationAccountName": "test-auto",
"dscConfigurationname": "dsc_baseconfig",
"dscConfigurationFile": "dsc_baseconfig.ps1",
"keyvaultName": "kv123"
}
PowerShell
# Import parameters from common JSON for reuse
# This file cannot have comments in it!
param(
[string]$paramFile = '.\dsc_parameters.json'
)
# For each object in the JSON, create a powershell variable
$params = Get-Content $paramFile | ConvertFrom-Json
$params.PSObject.Properties | ForEach-Object {
New-Variable -Name $_.Name -Value $_.Value -Force
}