Why alias does not work?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of X P X P 1 year, 9 months ago.

  • Author
    Posts
  • #22530
    Profile photo of X P
    X P
    Participant

    Hi Everyone! Thanks at first!

    I have an Issue, I don't understand this behavior.

    My Function Get-Test2 tell me the Manufacture, examples:

    PS C:\> Get-Test2 127.0.0.1
    

    Computername Manufacturer
    ———— ————
    127.0.0.1 Dell Inc.

    It's OK, this is what I want

    PS C:\> "127.0.0.1" | Get-Test2 
    

    Computername Manufacturer
    ———— ————
    127.0.0.1 Dell Inc.

    It's OK

    But, from the Pipeline Of a Get-adcomputer command.

    PS C:\> Get-ADComputer test | Get-Test2 
    

    Computername Manufacturer
    ———— ————
    CN=test,CN=Computers,DC=R,DC=LAN Dell Inc.

    I get

    DistinguishedName : CN=SI,CN=Computers,DC=R,DC=LAN,, I don't want DistinguishedName, I want DNSHostname

    I put an alias, but appears It doesn't work.

    This is my Psrameter:

            [Parameter(Mandatory=$true,
                       ValueFromPipeline=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
                       [Alias("DNSHostname")]
                    [string]$DNSHost
    
    

    Can anyone explain me this behavior?

    Thanks!

  • #22535
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    In short, the AD cmdlets are weird, and don't always play well with being piped directly into other commands. (Particularly those that use aliases.)

    Any easy workaround is to stick a call to Select-Object in the middle:

    Get-ADComputer test | Select-Object -Property * | Get-Test2
    

    It looks weird, but it works. Select-Object converts the objects that the AD cmdlet outputs into simple PSObjects with the same properties / values, and those PSObjects work fine when piped into other functions.

  • #22536
    Profile photo of X P
    X P
    Participant

    You are right! It WORKS! But looks weird.

    Thanks

You must be logged in to reply to this topic.