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 評価