ValueFromPipelineBypropertyName and alias not acting as expected

Welcome Forums General PowerShell Q&A ValueFromPipelineBypropertyName and alias not acting as expected

This topic contains 3 replies, has 3 voices, and was last updated by

 
Member
2 years, 9 months ago.

  • Author
    Posts
  • #34971

    Participant
    Points: 1
    Rank: Member

    I have created a function with a parameter name 'computername'. I want to accept value from pipeline, so I enabled ValueFromPipelineByPropertyName. Since I may be accepting pipeline info from get-adcomputer I set an alias to be 'Name".

    When I run either of these commands it works as expected:
    get-adcomputer -filter 'name -like "dc"' select name | my-function
    get-adcomputer -Filter 'name -like "trn_br1_w12"' | select @{n="ComputerName";e={$_.name}} | My-Function

    If I run this command it doesn't produce any output
    get-adcomputer -filter 'name -like "dc"' | my-function

    If I change the parameter name to 'Name' and the alias to 'ComputerName' the command works as expected and gives me the results.
    get-adcomputer -filter 'name -like "dc"' | my-function

    Shouldn't it work the same way for an alias as it does for the parameter?

    function My-Function
    {
    	[CmdletBinding()]
    	param
    	(
    		[Parameter(ValueFromPipeline = $true,
    				   ValueFromPipelineByPropertyName = $true)]
    		[Alias('Name')]
    		[string[]]$ComputerName,
    		[string]$LogName = '.\Deadfile.csv'
    	)
    
  • #34972

    Member
    Points: 0
    Rank: Member

    That's an annoying quirk of how the objects that the AD cmdlets output behave. An easy workaround is to pipe to "Select *" first:

    Get-ADComputer @adComputerParams | Select * | My-Function
    

    There's nothing else you can do about it, unfortunately. The AD module is very, very weird.

  • #35151

    Participant
    Points: 0
    Rank: Member

    Interesting, I think the difference between within and without "Select" is the membertype is changed from property to noteproperty though the object type is still ADComputer.

    Does it mean why we pipebypropertyname, we should always make sure it's noteproperty?

  • #35154

    Member
    Points: 0
    Rank: Member

    Nope, most of the time there's no need to worry about that. The AD objects are just weird.

The topic ‘ValueFromPipelineBypropertyName and alias not acting as expected’ is closed to new replies.