0

Currently I'm doing like that:

function Invoke-Service
{
    Param(
        [parameter(Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [int] $Id,
        [parameter(Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [string] $Name,
        [parameter(Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [int] $Age
    )
    DoSomeWork |
        New-Object PSObject -Property @{ Id = $Id; Name = $Name; Age = $Age }
}

This function can obtain it's parameters directly or from Import-Csv output, or select output.

But very often I want to continue processing down the pipeline with all the power of PSObject:

Import-Csv -Path "data.csv" |
    Invoke-Service |
        ... #

And my question is: do I need to call New-Object enumerating all parameters or is there a keyword or other technique which I missed?

astef
  • 8,575
  • 4
  • 56
  • 95

1 Answers1

2

Use the $PSBoundParameters variable:

New-Object psobject -Property $PSBoundParameters

$PSBoundParameters is an automatic variable that contains a hashtable with an entry per named parameter, using the full parameter name as the key and the parameter argument as the value, so in your example it's already 100% aligned with the input you're trying to pass

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206