Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

advanced function (script cmdlets) robuste Parameterbehandlung/verarbeitung von pipeline-unterstützenden Parametern

$
0
0

Hallo zusammen,

gibt es ein Best Practice, wie die Parameterbehandlung in einer advanced function mit process block erfolgen soll, so dass der pipeline-unterstützende Parameter unabhängig von der Inputvariante (direct vs. pipeline) gleich behandelt/verarbeitet wird?

Die übliche Variante der Aufnahme von "foreach ($inp in $inputObject) { Do something with $_ } " erzeugt unterschiedliches Verhalten, wenn z.B. ein Array von Arrays als direct bzw. pipeline Input übergeben wird. Dazu folgendes Beispiel:

Set-StrictMode -Version Latest

$inp_array = @(("1","2"),("3","4"))
Test-InputHandling $inp_array # direct call
$inp_array | Test-InputHandling # pipeline call

function Test-InputHandling
{
  [CmdletBinding()]
  param(
    [Parameter(ValueFromPipeline=$TRUE)]
    [string[]] $inputObject = "default"
    )

    begin
    {
        $callLine = (Get-PSCallStack)[0].InvocationInfo.Line
        write-output "--- Function call: $callLine ---"
        $n = 0 # counter for objects passed to process block
    }

    process
    {
        write-output "in process block..."
        $inputObject | ForEach-Object { $_ }
        $n++
    }

    end
    {
        Write-Host "-> Input received as $n item(s)"
    }

}

Das Ergebnis unterscheidet sich je nach Aufrufvariante direct vs. pipeline.

Vielen Dank schon mal vorab


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>