Pipeline Input Always Coercing to String

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?

June 25th, 2015 6:51pm

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?

>> My problem is that the third invocation, passing a Process object via the pipeline, is always getting coerced as a string

True!

It's always being coerced to a string by you, through your code: ..."proc: $proc"

$proc is [System.Diagnostics.Process], so, you could use $proc.HandleCount, $proc.CPU, $proc.Description if you'd like.

But your code is equivalent to $proc.ToString(), which returns a ...


Free Windows Admin Tool Kit Click here and download it now
June 26th, 2015 5:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics