-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Summary of the new feature / enhancement
Static parameters can easily be defined declarative inside a param() structure.
Dyamic parameter is not directly supported and must be programmed inside a dynamicparam section of a scriptblock. This requires deep knowledge of intrinsic types and concepts.
By adding a new attribute [Dynamic()], parameters can easily be declared as being dynamic. The attribute could take a scriptblock as argument to specify the condition under which the dynamic parameter would show.
param
(
# regular static parameter
[string]
$Normal,
# show -Lunch only at 11 a.m. or later
[Dynamic({(Get-Date).Hour -ge 11})]
[switch]
$Lunch,
# show -Mount only when -Path refers to a local path (and not a UNC path)
[string]
$Path,
[Dynamic({$PSBoundParameters['Path'] -match '^[a-z]:'})]
[switch]
$Mount
)Here is a working example and a proof of concept (hosted in the gallery) that shows how dynamic parameters could be leveraged this way:
https://github.com/TobiasPSP/Modules.dynpar/blob/main/README.md
It would be awesome if both the attribute [Dynamic()] and the auto-generation of the code would one day be natively integrated in PowerShell so users would just need to choose which of their parameters should be dynamic, and when.
Proposed technical implementation details (optional)
Technical implementation available here: https://github.com/TobiasPSP/Modules.dynpar/blob/main/README.md