Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with two "accept from pipeline" parameters

Tags:

powershell

I want a function to allow me to pass in either a device ID or a display name, and to do stuff with it.

In the following example, I pass in a customer PowerShell object that only contains a device ID ($obj.ID | Test-Function), but both $DisplayName and $Id end up with that value.

How do I force the value into the correct parameter?

function Test-Function {
[CmdletBinding()]
    Param (
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [string]$DisplayName

        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [string]$Id
    )
    Begin {
        #Code goes here
    }
    Process {
        Write-Host "displayname is: $DisplayName" -ForegroundColor Green
        Write-Host "displayname is: $Id" -ForegroundColor Green        
    }
}
like image 802
StackExchangeGuy Avatar asked Mar 28 '26 00:03

StackExchangeGuy


2 Answers

You can solve this with ParameterSets. Notice I also fixed a comma in your code and the Write-Host output:

function Test-Function 
{
[CmdletBinding()]
    Param (
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            ParameterSetName='DisplayName'
        )]
        [string]$DisplayName,


        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            ParameterSetName='Id'
        )]
        [string]$Id
    )
    Begin {
        #Code goes here
    }
    Process {
        Write-Host "displayname is: $DisplayName" -ForegroundColor Green
        Write-Host "Id is: $Id" -ForegroundColor Green        
    }
}

Lets give it a try:

[PsCustomObject]@{Id = "hello"} | Test-Function

Outputs:

displayname is: 
Id is: hello

and

[PsCustomObject]@{DisplayName = "hello"} | Test-Function

outputs

displayname is: hello
Id is: 
like image 183
Martin Brandl Avatar answered Mar 29 '26 14:03

Martin Brandl


Just remove ValueFromPipeline and set $false for Mandatory attributes , so the correct solution is:

function Test-Function {
[CmdletBinding()]
    Param (
        [Parameter(
            Mandatory=$false,
            ValueFromPipelineByPropertyName=$true
        )]
        [string]$DisplayName,

        [Parameter(
            Mandatory=$false,
            ValueFromPipelineByPropertyName=$true
        )]
        [string]$Id
    )
    Begin {
        #Code goes here
    }
    Process {
        Write-Host "displayname is: $DisplayName" -ForegroundColor Green
        Write-Host "displayname is: $Id" -ForegroundColor Green        
    }
}
like image 22
TWEESTY Avatar answered Mar 29 '26 12:03

TWEESTY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!