在 PowerShell 中,您可以将多个参数传递给函数或 cmdlet,方法是将它们包装在哈希表变量中,然后传递前缀为该变量@
代替$
.
是否可以使用作为另一个对象的属性的哈希表(即作为一个衬垫)进行splat?例如下面我首先必须分配属性(testInt
, testString
) 到另一个变量,然后我可以将其展开到Demo
。我想要某种方法来避免这个额外的步骤,但一直无法找到一个简洁的解决方案......
function Demo {
[CmdletBinding()]
Param (
[Parameter()]
[string]$One
,
[Parameter()]
[string]$Two
)
"1 = $One"
"2 = $Two"
}
$test = @{
testInt = @{
One = '1'
Two = '2'
}
testString = @{
One = 'One'
Two = 'Two'
}
}
$t = $test.testInt
Demo @t
$t = $test.testString
Demo @t
Demo @test.testInt #this doesn't work / I've also tried similar options with various castings and braces though to no avail.
Update
提交建议:https://github.com/PowerShell/PowerShell/issues/5227 https://github.com/PowerShell/PowerShell/issues/5227
AFAIK splat 对象属性或数组或哈希表元素的唯一方法是将其分配给变量,然后 splat 该变量
$p = $variable.property
demo @p
符号@variable.property
是无效的,并且两者都不是@()
nor @{}
可以用来允许这样的语法,因为它们已经具有不同的含义(数组构造和哈希表构造)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)