PSSnow icon indicating copy to clipboard operation
PSSnow copied to clipboard

Table dynamic params do not pull default values

Open insomniacc opened this issue 2 years ago • 2 comments

I'll explain with a possible use case: Get-SNOWObject is used as a template function for all other Get-SNOW* (table) commands. It's parameters are pulled through to form the framework of those other functions (into the dynamic param block) Default values are not pulled through as it's not part of the 'Get-Command' dataset.

Now lets say that by default, we wanted all Get-SNOW* functions to default the -DisplayValue to 'true'. I could set this in the Get-SNOWObject command, but it would not apply to all others templating from this.

Possible Solution: Ast parsing

This is not currently presenting an issue, although might be a nice to have for the use case specified above.

insomniacc avatar Mar 12 '23 18:03 insomniacc

This might also be a better method for parsing the script block pushed into Invoke-SNOWBatch, rather than using the current regex method. It's something to explore anyway.

insomniacc avatar Mar 12 '23 18:03 insomniacc

I've tested Import-DefaultParamSet with the following additional code:

$Parsed = [System.Management.Automation.Language.Parser]::ParseInput($BaseCommand.Definition,[ref]$null,[ref]$null)
$ParsedParams = $parsed.FindAll({$args[0] -is [System.Management.Automation.Language.ParameterAst]},$true)

Within the section that loops through the get-command $BaseCommand.Parameters, I'm also doing the following:

$ParsedParam = $null
$ParsedParam = $ParsedParams.Where({$_.Name.ToString() -eq "`$$key"})
if($ParsedParam -and $null -ne $ParsedParam.DefaultValue){
    $param.Value = $ParsedParam.DefaultValue.Value
}

Unfortunately using this method the default value that's being defined is not available to use. I found a similar issue here https://stackoverflow.com/questions/11012514/how-to-set-default-value-for-dynamic-param $PSBoundParameters was used as a workaround. This would not work in this instance since it's nested inside a helper function.

I'm not sure if there's an alternative on how to do this in the way that I'm hoping for, but for now I don't have a specific requirement that needs this, it would just be a nice to have. Going to stick it on the back burner until either something new comes up or I decide to shelve it.

insomniacc avatar Apr 03 '23 13:04 insomniacc