-1

I'm writing a PowerShell script that can take 2 parameters - Computer name or Serial number - and output some information about the corresponding computer. However, when I pass a value in from the pipeline, it always goes into the wrong parameter.

Here's my code:

Param
(
    [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True)]
    [String]$ComputerName,
    [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True)]
    [String]$SerialNumber
)

Then it uses those values to pull information from SCCM.

However, if I use the following line to call the script:

"<ComputerName>" | .\Get-SerialNumber.ps1 -ComputerName $_

The computer name gets put into the SerialNumber parameter instead of the ComputerName parameter.

If I put in

"<SerialNumber>" | .\Get-SerialNumber.ps1 -SerialNumber $_

The value gets passed into the ComputerName parameter. What is going on here? Why are they being passed backwards?

If I type in

.\Get-SerialNumber.ps1 -SerialNumber "<SerialNumber>"

or

.\Get-SerialNumber.ps1 -ComptuerName "<ComputerName>" 

it works fine. It only screws up when values are passed through the pipeline.

For the record, I've tried to restart my PowerShell session to make sure something weird wasn't happening in my session, and even tried on another computer entirely. It still happens the same way.

Randy
  • 1,068
  • 2
  • 14
  • 32
  • @briantist This is NOT a duplicate of the question that was linked. I just looked at that question/answer, my question was not answered as part of that. That question shows on a very basic level what the pipeline does, this was a more advanced level of pipeline usage. – Randy Jun 16 '17 at 19:30
  • I disagree; your question showed a misunderstanding of basic pipeline usage, which is what the linked duplicate provides. You were trying to do something invalid, not an advanced usage. – briantist Jun 16 '17 at 19:36
  • @briantist yes, it was a misunderstanding, which is a large part of what this site is for. However, my misunderstanding is in no way described in the link provided. I would have had no more understanding of what I was trying to do after reading that question and its answers. The comments on the answer below DID, on the other hand, help with my understanding of this specific concept and provided me with an actual answer to my question. – Randy Jun 16 '17 at 19:45
  • The accepted answer on the linked question shows how to write a pipeline capable function, how to use `$_` in a pipeline function, how to use the `process` block. I think that question and answer will ultimately be more useful to future readers. I was actually going to delete my answer (and may still) once I realized this was a duplicate, but held off because of the comment discussion. In any case if PetSerAl or someone else wants to reopen this and post a full answer I won't try to interfere. – briantist Jun 16 '17 at 19:53

1 Answers1

1

The way you're calling these doesn't make sense. If you pass in a ComputerName via the pipeline, and then also specify -ComputerName as a named parameter, of course it binds the pipeline to the only other parameter that accepts pipeline input. How did you expect to accept two different values for the same parameter? How would you use it?

briantist
  • 45,546
  • 6
  • 82
  • 127
  • Shouldn't the `$_` represent the value that is being passed through the pipeline? wouldn't `-ComputerName $_` mean assign the value from the pipeline to the ComputerName parameter? – Randy Jun 16 '17 at 18:12
  • @Randy No, not at all. You must use a `Process { }` block in your function and then within that process block, `$ComputerName` would refer to the current pipeline object. The `Process` block gets called once for each item in the pipeline. – briantist Jun 16 '17 at 18:14
  • 2
    *Shouldn't the `$_` represent the value that is being passed through the pipeline?* Yes it represent value from *outer* pipeline: `"Outer pipeline" | % { "" | .\Get-SerialNumber.ps1 -ComputerName $_ }`. You although can write it like this: `"" | .\Get-SerialNumber.ps1 -ComputerName { $_ }`, but it will not prevent binding pipeline values to other `ValueFromPipeline` parameters. – user4003407 Jun 16 '17 at 18:53
  • Thank you @PetSerAl. That makes sense. – Randy Jun 16 '17 at 18:58