Dynamically Set Powershell Variables from json
powershell tech sql-server
I created this small snippet to allow a list of values from a json file be turned into variables to work with. For working with a fixed list of configuration values, this might be helpful to reduce some coding effort.
Basic Setting of Variables from JSON object via powershell that's dynamic
#get json content (should not be nested, just flat file of variables)
$json = Get-Content -Raw -Path ([io.path]::Combine($Path,"settings.json")) -force | ConvertFrom-Json
#get list of variable in collection available
[string[]]$variables = ($json | get-member -Name * -MemberType NoteProperty).Name
#iterate through each variable name and get the value from the json object
foreach ($v in $variables)
{
Set-Variable -name $v -value ($json.$v) -Force -Verbose
}