Joh*_*van 2 powershell splat powershell-5.0
在 PowerShell 中,您可以将多个参数传递给函数或 cmdlet,方法是将它们包装在一个哈希表变量中,然后传递前缀为@而不是 的变量$。
是否可以使用哈希表作为另一个对象的属性(即作为一个衬垫)?例如,下面我首先必须将属性 ( 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.
Run Code Online (Sandbox Code Playgroud)
更新
提交的建议:https : //github.com/PowerShell/PowerShell/issues/5227
AFAIK splat 对象属性或数组或哈希表元素的唯一方法是将其分配给一个变量,然后 splat 该变量
$p = $variable.property
demo @p
Run Code Online (Sandbox Code Playgroud)
该符号@variable.property是无效的,@()也@{}不能用于允许这种语法,因为它们已经具有不同的含义(数组构造和哈希表构造)。
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |