简体   繁体   中英

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 :) ).

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:

PowerShell中参数绑定的堆栈跟踪

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM