考虑以下简单函数:
function Write-HostIfNotVerbose()
{
if ($VerbosePreference -eq 'SilentlyContinue')
{
Write-Host @args
}
}
它工作正常:
现在我想让它成为一个高级函数,因为我希望它继承详细程度偏好:
function Write-HostIfNotVerbose([Parameter(ValueFromRemainingArguments)]$MyArgs)
{
if ($VerbosePreference -eq 'SilentlyContinue')
{
Write-Host @MyArgs
}
}
但它不起作用:
让我抓狂的是,我无法确定$args
第一个示例与第二个示例有何不同$args
。
我知道@args
默认情况下本机飞溅不适用于高级功能 - https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.2#notes
但我希望它可以被模拟,但它也不起作用。我的问题是 - 我试图模拟它的方式有什么问题以及是否可以在不显示所有Write-Host
参数的情况下修复我的代码Write-HostIfNotVerbose