我正在编写一个可以在管道中间调用的 Cmdlet。使用此 Cmdlet,有些参数具有 ValueFromPipelineByPropertyName 属性,以便 Cmdlet 可以使用与管道中之前定义的名称相同的参数。
我遇到的悖论是在被覆盖的 BeginProcessing() 方法中,我利用可以从管道获取其值绑定的参数之一。根据 Cmdlet 处理生命周期,绑定直到BeginProcessing() 被调用。因此,如果尝试在 BeginProcessing()。
我考虑过将内容移至 ProcessRecord() 方法。不幸的是,需要进行一项相对昂贵的一次性操作。发生这种情况的最佳位置似乎是在 BeginProcessing() 方法可帮助确保它仅在管道中发生一次。
围绕此问题的几个问题问题:
- 有好的解决方法吗?
-
这些相同的参数也有 强制 对它们设置属性。我怎样才能做到这一点而不让 PowerShell 抱怨没有这些必需的参数?
提前感谢您的想法。
更新
我拿出了问题的第二部分,因为我意识到我对管道绑定参数的理解不够好。我错误地认为管道绑定参数来自于管道中执行的先前 Cmdlet。实际上来自通过管道传递的对象!我引用了 Keith Hill 的帖子来帮助理解这一点。
I'm writing a Cmdlet that can be called in the middle of a pipeline. With this Cmdlet, there are parameters that have the ValueFromPipelineByPropertyName attribute defined so that the Cmdlet can use parameters with the same names that are defined earlier in the pipeline.
The paradox that I've run into is in the overridden BeginProcessing() method, I utilize one of the parameters that can get its value bound from the pipeline. According to the Cmdlet Processing Lifecycle, the binding of pipeline parameters does not occur until after BeginProcessing() is called. Therefore, it seems that I'm unable to rely on pipeline bound parameters if they're attempting to be used in BeginProcessing().
I've thought about moving things to the ProcessRecord() method. Unfortunately, there is a one time, relatively expensive operation that needs to occur. The best place for this to happen seems to be in the BeginProcessing() method to help ensure that it only happens once in the pipeline.
A few questions question surrounding this:
- Is there a good way around this?
- These same parameters also have the Mandatory attribute set on them. How can I even get this far without PowerShell complaining about not having these required parameters?
Thanks in advance for your thoughts.
Update
I took out the second part of the question as I realized I just didn't understand pipeline bound parameters well enough. I mistakingly thought that pipeline bound parameters came from the previous Cmdlet that executed in the pipeline. The actually come from the object being passed through the pipeline! I referenced a post by Keith Hill to help understand this.
发布评论
评论(1)
您可以在 BeginProcessing 中将实例字段 bool (Init) 设置为 false。然后检查BeginProcessing中是否设置了该参数。如果是,则调用执行一次性初始化的方法 (InitMe)。在 ProcessRecord 中检查 Init 的值,如果为 false,则调用 InitMe。 InitMe 应在返回之前将 Init 设置为 true。
关于第二个问题,如果您已将该参数标记为强制参数,则必须将其作为参数或通过管道提供。您是否使用多个参数集?如果是这样,那么即使某个参数被标记为强制参数,只有当关联的参数集是 PowerShell 确定用于 cmdlet 的特定调用的参数集时,该参数才是强制的。
You could set an instance field bool (Init) to false in BeginProcessing. Then check to see if the parameter is set in BeginProcessing. If it is then call a method that does the one time init (InitMe). In ProcessRecord check the value of Init and if it is false, then call InitMe. InitMe should set Init to true before returning.
Regarding your second question, if you've marked the parameter as mandatory then it must be supplied either as a parameter or via the pipeline. Are you using multiple parameter sets? If so, then even if a parameter is marked as mandatory, it is only mandatory if the associated parameter set is the one that is determined by PowerShell to be in use for a particular invocation of the cmdlet.