Validation

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 11 months ago.

  • Author
    Posts
  • #21577
    Profile photo of
    Anonymous

    Hi Team,
    Could you please tell me how can I get to know how the input has been given? what I mean is...

    1. by .txt file
    gc input.txt | %{Verb-Noun $_}
    2. or CSV file
    Import-Csv input.csv | %{Verb-Noun $_.input}
    3. or direct input to the parameter?
    Verb-Noun -InputParam "InputItem"

  • #21582
    Profile photo of Don Jones
    Don Jones
    Keymaster

    From within the command "Verb-Noun," the input on 1 and 2 arrives from the pipeline. You don't know how the data got into the pipeline, and you can't know.

    For 3, input was provided directly to a parameter. You could use the $PSBoundParameters collection.

    In many cases, if you write your function correctly, it doesn't matter where the input came from.

  • #21611
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In this particular case, as far as your Verb-Noun function is concerned, all three examples are "direct input to the parameter". The first two examples happen to be using positional binding instead fo a named parameter, but either way, you're not actually piping input to Verb-Noun. You're piping it to ForEach-Object, and then calling Verb-Noun multiple times from there.

    While you 'might' be able to pull the information you're asking about from either $MyInvocation or Get-PSCallStack, your Verb-Noun command really shouldn't care where the input comes from, and trying to figure that out from inside Verb-Noun will seriously clutter your code. I'd be asking myself why I felt I needed that information in the first place, and see if I can find some other approach.

You must be logged in to reply to this topic.