Send pipeline input by name to inappropriate property name

I have a function that creates an object named property "Incident number":

Function Get-ID {
    New-Object -TypeName PSObject -Property @{'Incident Number' = 'INC12345'; 'Other Stuff' = 'Blah'}
}

      

I want to pipe this Incident Number value to another function down the pipeline, but in the second function, the input property is called "ID":

Function Get-Note {
    Param(
        [Parameter(ValueFromPipeline)]
        [string]$ID
    )
    Write-Output "The input was $ID"
}

      

Although it is possible to have a variable name with spaces (for example ${Incident Number}

), this makes it awkward to use as a command line parameter.

As defined above, the output is:

The input was @ {Other Stuff = Blah; Incident Number = INC12345}

Usage [Parameter(ValueFromPipelineByPropertyName)]

returns:

Get-Note: The input object cannot be bound to any parameters for either because the command does not accept pipeline input or input and its properties do not match any of the parameters that accept pipeline input.

FYI - The above example. In fact, the properties of the object created by the first function are determined by the API call. While I could rename these properties as part of this function, I prefer to store them as specific APIs.

+3


source to share


1 answer


I found that it can be done using the parameter attribute [Alias()]

:

Function Get-ID {
    New-Object -TypeName PSObject -Property @{'Incident Number' = 'INC12345'; 'Other Stuff' = 'Blah'}
}

Function Get-Note {
    Param(
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [Alias('Incident Number')]
        [string]$ID
    )
    Write-Output "The input was $ID"
}

      



Output:

PS> Get-ID | Get-Note

The input signal was INC12345

+4


source







All Articles