Interesting “gotcha” with dynamic parameters and pipeline input

I was working on a tricky problem with Pester’s mocking functionality today, and came across something that I didn’t previously know about how dynamic parameters work in PowerShell. Dynamic parameters are generated before (and only before) pipeline input is processed.

This means that if your dynamicparam block depends on the values of a parameter that happens to accept pipeline input, you won’t get your dynamic parameters when a user chooses to use that pipeline input functionality.

This is true of both compiled Cmdlets, and Advanced Functions. For example:

# This works fine:
Get-ChildItem -Path 'Cert:\' -CodeSigningCert -Recurse

# And this generates an error that the CodeSigningCert
# parameter cannot be found:
'Cert:\CurrentUser' | Get-ChildItem -CodeSigningCert -Recurse

About Dave Wyatt

Microsoft MVP (PowerShell), IT professional.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s