PowerShell在分配从管道获得的新值之前,会读取标有ValueFromPipeline
或的参数的默认值ValueFromPipelineByPropertyName
进行备份。
请考虑以下 cmdlet:
New-Post -Title <string>
Set-Post -InputObject <Post> -Title <string>
以下情况适用:
New-Post
cmdlet将新创建的对象返回到具有属性的管道Post
Title
- cmdlet 上的
InputObject
属性Set-Post
标记为ValueFromPipeline = true
- cmdlet 上的
Title
属性Set-Post
标有ValueFromPipelineByPropertyName = true
。
将它们与以下命令结合使用:
New-Post -Title "Foo" | Set-Post
Set-Post
并在cmdlet属性的 get 访问器上设置断点会Title
导致以下堆栈跟踪:

如您所见,在将cmdlet 上的属性与来自管道的对象的相应属性的值CmdletParameterBinderController.GetDefaultParameterValue
绑定的过程中调用该方法。Title
Set-Post