gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:46:08 25 4
gpt4 key购买 nike

我正在尝试为 Pester 的 Should 创建一个包装器(代理)命令。可能的用例包括 transparent logging of test input even in case of successimprove the way Pester logs objects of certain types, e. g. hashtable .

因为 Should 是一个高级函数,所以通过 $args splatting 转发参数是行不通的。

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

$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 namedparameters. One or more parameters issued cannot be used together oran insufficient number of parameters were provided.

看起来包装器生成器不理解 dynamicparam declaration of Should .

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

最佳答案

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

关于powershell - 如何为使用动态参数的高级函数 cmdlet 创建包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65627872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com