Powershell: Salpicar una propiedad

Creado en 25 oct. 2017  ·  3Comentarios  ·  Fuente: PowerShell/PowerShell

Resumen

Sería útil si pudiéramos utilizar la propiedad de una variable (es decir, donde esa propiedad es en sí misma una tabla hash) a un comando.

Ejemplo de código:

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)

Esto podría ser particularmente útil cuando se trata de archivos de configuración. Por ejemplo, nuestra configuración contiene información de la base de datos (instancia, catálogo, credenciales) y también secciones con otras propiedades específicas de nuestras funciones. p.ej

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

Comentario más útil

Parece que necesitamos revivir https://github.com/PowerShell/PowerShell-RFC/issues/6 : el borrador de RFC sugiere una representación de expresión generalizada, con una sintaxis como:

Test-Demo @$test.testString

Todos 3 comentarios

Parece que necesitamos revivir https://github.com/PowerShell/PowerShell-RFC/issues/6 : el borrador de RFC sugiere una representación de expresión generalizada, con una sintaxis como:

Test-Demo @$test.testString

@JohnLBevan ¿Podemos cerrar el problema buscando https://github.com/PowerShell/PowerShell-RFC/issues/6 ?

@iSazonov ; sí, feliz de que esto se cierre dado que ya está cubierto allí. Gracias.

¿Fue útil esta página
0 / 5 - 0 calificaciones