Powershell: 泼财产

创建于 2017-10-25  ·  3评论  ·  资料来源: PowerShell/PowerShell

概括

如果我们可以将变量的属性(即该属性本身是一个哈希表)用于命令,那将会很有帮助。

代码示例:

function Test-Demo {
    [CmdletBinding()]
    Param (
        [Parameter()]
        [string]$One
        ,
        [Parameter()]
        [string]$Two
    )
    "1 = $One"
    "2 = $Two"
}
$test = @{
    testInt = @{ 
        One = '1'
        Two = '2'
    }
    testString = @{
        One = 'One'
        Two = 'Two'
    }
}

#to splat a property, we first need to assign the property to a "top level" variable:
$t = $test.testString
Test-Demo <strong i="10">@t</strong> 

#however it would be nice if we could instead do this:
Test-Demo @test.testString 

#or if that's not possible for some reason, maybe something like this:
Test-Demo ([splat]$test.testString)

这在处理配置文件时特别有用。 例如,我们的配置包含数据库信息(实例、目录、凭据),以及具有特定于我们的功能的其他属性的部分。 例如

$config = Get-MyScriptConfig -Path '.\Config.xml'
Invoke-Something @config.DatabaseConnectionInfo @config.Something
Issue-Question Resolution-Answered

最有用的评论

听起来我们需要重振https://github.com/PowerShell/PowerShell-RFC/issues/6 - RFC 草案建议使用广义表达式,语法如下:

Test-Demo @$test.testString

所有3条评论

听起来我们需要重振https://github.com/PowerShell/PowerShell-RFC/issues/6 - RFC 草案建议使用广义表达式,语法如下:

Test-Demo @$test.testString

@JohnLBevan我们可以关闭看https://github.com/PowerShell/PowerShell-RFC/issues/6的问题吗?

@iSazonov ; 是的,很高兴关闭它,因为它已经在那里覆盖了。 谢谢。

此页面是否有帮助?
0 / 5 - 0 等级