I want to write a script with parameters, where the default values for those parameters are stored in a json file. Unfortunately, Powershell does not recognise param() if it is not the first line of code within the script.
But of course if it is the first line of the script, it cannot load the config in advance and use its value as default value. So I am in anpickle.
The config.json:
{"defaultValue":"Default"}
The powershell script:
$configPath = "config.json"
$configPath = Join-Path $PSScriptRoot $configPath
# read config
$config = Get-Content $configPath | ConvertFrom-Json
write-host $config.defaultValue
param($myparam = $config.defaultValue)
write-host $myparam
I am new to powershell, is there a way to set default values of parameters later in the script? Or alternatively, read parameters later into the script?