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.