Powershell: Help invokes DynamicParam 4 times

Created on 5 Oct 2020  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Function Test {
    [CmdletBinding(DefaultParameterSetName = 'DefaultParameterSet')] param()
    DynamicParam {
        Write-Host 'Test'
    }
}

Expected behavior

Test should only appear once when using the help like: Test -?

Actual behavior

Test -?

Test
Test
Test
Test

NAME
    test

SYNTAX
    test [<CommonParameters>]


ALIASES
    None


REMARKS
    None

I don't think this is a big deal but has a performance impact on the help functions

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question WG-Engine WG-Engine-Performance

Most helpful comment

This happens in other places, If you call your function Test-Thing test- [tab] will also cause the dynamic parameters to be evaluated. I've seen this where a module has hundreds of dynamic params each making multiple calls to a server. Tab got really slow. It's one more reason to avoid them

All 4 comments

Oh, PowerShell sometime re-evaluates things ... This is not always a problem. This is not a problem in this example (until dynamic parameter requests expensive resource). If this was during the work of the pipeline (on hot path), then this would be a problem - have you an example with hot path?

No, I have no example of a hot path. I was just experiencing a slightly longer delay (in e.g. the WhereObject example of #13746) using the -? option in comparison with using no/another option. It is also a bit confusing while debugging dynamic parameters.

You could investigate to ensure there is not extra calls. But I expectation it comes from DynamicParam nature - dependency on current context.

This happens in other places, If you call your function Test-Thing test- [tab] will also cause the dynamic parameters to be evaluated. I've seen this where a module has hundreds of dynamic params each making multiple calls to a server. Tab got really slow. It's one more reason to avoid them

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximoTrinidad picture MaximoTrinidad  路  3Comments

concentrateddon picture concentrateddon  路  3Comments

andschwa picture andschwa  路  3Comments

manofspirit picture manofspirit  路  3Comments

SteveL-MSFT picture SteveL-MSFT  路  3Comments