I have a dynamically loaded PowerShell script block that accepts multiple parameters (including Mandatory, type, default value,...). I also have a function that has its own parameter sets and internally, it calls the script block. The script block is dynamically loaded based on other parameters, and I'd like to allow the user to pass the parameters to my function and forward them to script block. For that, I'd like to read the possible parameters from the script block, which I can do using $block.Ast.ParamBlock.Parameters
and then create matching parameters for my function using DynamicParam
.
Example:
Script block:
$block = {
param([Parameter(Mandatory)][string]$test = 10)
echo $test
}
Function:
function Fn {
param($FnParam1, $FnParam2)
DynamicParam {
// what to write here
}
& $block // how to efficiently forward the parameters here?
}
It should be possible to call Fn
like this:
Fn -FnParam1 value -FnParam2 value -test 20
However, DynamicParam
is created using RuntimeDefinedParameter
and related classes, and AST of the script block returns Management.Automation.Language.ParameterAst
and other instances from the same namespace. I could manually create the DynamicParam
parameters by converting the AST node-by-node, but it seems like there should be a simpler way to automatically convert these.