Author Posts

January 3, 2015 at 9:08 pm

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"

January 4, 2015 at 7:21 am

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.

January 5, 2015 at 5:58 am

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.