Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare get accessors in PowerShell cmdlet parameters

According to MSDN:

"Parameters must be declared on public non-static fields or properties. Parameters should be declared on properties. The property must have a public set accessor, and if the ValueFromPipeline or ValueFromPipelineByPropertyName keyword is specified, the property must have a public get accessor."

Why do I have to declare get accessors in my cmdlet ValueFromPipeline parameters? As far as I know, PowerShell only needs to put their values in, not read them out. Thanks (by the way im just curious about this behavior :) ).

like image 570
resgh Avatar asked Oct 23 '22 02:10

resgh


1 Answers

PowerShell reads the default value of the parameters marked with ValueFromPipeline or ValueFromPipelineByPropertyName to make a backup before assigning the new value obtained from the pipeline.

Consider the following cmdlets:

New-Post -Title <string>
Set-Post -InputObject <Post> -Title <string>

where the following applies:

  • the New-Post cmdlet returns the newly created Post object to the pipeline, which has a Title property
  • the InputObject property on the Set-Post cmdlet is marked with ValueFromPipeline = true
  • the Title property on the Set-Post cmdlet is marked with ValueFromPipelineByPropertyName = true.

Combining them with the following command:

New-Post -Title "Foo" | Set-Post

and setting a breakpoint on the get accessor of the Set-Post cmdlet's Title property results in the following stack trace:

Stack trace for parameter binding in PowerShell

As you can see, the CmdletParameterBinderController.GetDefaultParameterValue method is invoked during the process of binding the Title property on the Set-Postcmdlet with the value from the corresponding property on the object coming from the pipeline.

like image 84
Enrico Campidoglio Avatar answered Oct 30 '22 17:10

Enrico Campidoglio