I have a very simple Cmdlet that has two parameter sets: one that accepts a string from the pipeline and the other that accepts a System.Diagnostics.Process from the pipeline. Both are required parameters are required, I just would like to be able to call the cmdlet with either type from the pipeline. When a string is passed in, however, the process is not mandatory. For example, I would like all of these scenarios to work:
"Hello" | My-Cmdlet $(Get-Process)[0] "Hello" | My-Cmdlet (Get-Process)[0] | My-Cmdlet "Hello"
My problem is that the third invocation, passing a Process object via the pipeline, is always getting coerced as a string. The actual code that I am writing is much more complex, but this example boils down what I'm trying to do:
function My-Cmdlet { [CmdletBinding(DefaultParameterSetName="Set1")] param( [Parameter(Mandatory=$false, ParameterSetName="Set2")] [Parameter(Mandatory=$true, ValueFromPipeline=$True, ParameterSetName="Set1")] [System.Diagnostics.Process]$proc, [Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="Set2")] [Parameter(Mandatory=$True, Position=0, ParameterSetName="Set1")] [string]$strParam ) PROCESS { Write-Host "strParam: $strParam" Write-Host "proc: $proc" } }
I've read about parameter transformations, but that seems like overkill for the simple task I'm trying to accomplish. Isn't there a better/right way?