如何为使用动态参数的高级函数 cmdlet 创建包装器

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

我正在尝试为 Pester 的 Should cmdlet. Possible use cases include transparent logging of test input even in case of success and 创建一个包装器(代理)。

由于 Should 是一个高级函数,通过 $args splatting 转发参数不起作用。

所以我尝试使用 System.Management.Automation.ProxyCommand::Create() 生成包装器,如 :

所述
$cmd = Get-Command Should
$wrapperSource = [System.Management.Automation.ProxyCommand]::Create( $cmd )
$wrapperSource >should_wrapper.ps1

调用包装器时,Powershell 输出此错误消息:

Should: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

包装器生成器似乎不理解 dynamicparam declaration of Should

如何在不复制 Pester 代码的情况下为 Pester 的 Should 编写通用包装器?

It looks like the wrapper generator doesn't understand the dynamicparam declaration of Should.

包装生成器默认忽略dynamicparam。幸运的是,这可以通过一些模板轻松解决:

$cmd = Get-Command Should
$pct = [System.Management.Automation.ProxyCommand]
$wrapperSource = @(
  $pct::GetCmdletBindingAttribute($cmd)
  'param('
    $pct::GetParamBlock($cmd)
  ')'
  'dynamicparam {'
    $pct::GetDynamicParam($cmd)
  '}'
  'begin {'
    $pct::GetBegin($cmd)
  '}'
  'process {'
    $pct::GetProcess($cmd)
  '}'
  'end {'
    $pct::GetEnd($cmd)
  '}'
) -join [Environment]::NewLine