Function Alias Question

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Alex Alex 6 months, 3 weeks ago.

  • Author
    Posts
  • #38818
    Profile photo of Alex
    Alex
    Participant

    I am unsure why something like this does not work:

    I have a function e.g.:

    
    function get-name
    {
        [CmdletBinding()]
        Param
        (
            # Param1 help description
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [alias("SamAccountName")]
            $myname
        )
    
        Begin
        {
        }
        Process
        {
        }
        End
        {
            echo $myname
        }
    }
    

    Load this function, and then try:

    get-aduser "name" | get-name
    

    This returns nothing.
    Change the Parameter var to $samaccountname it then returns the samaccountname (from the pipeline).
    I thought have the alias there should allow it to return that value from the pipeline?
    Just wondering if this is something that I have misunderstood?

  • #38829
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Welcome to the "People annoyed by the ActiveDirectory" module club. I'm not only the president, I'm also a member! 🙂

    Seriously, this would work with literally any other command piped to your function _except_ those in the AD module. Those objects are just weird, but there is a workaround:

    get-aduser "name" | select * |  get-name
    

    It looks silly to stick "select *" in the pipeline like that, but it produces an object that is much better behaved than the original.

  • #38831
    Profile photo of Alex
    Alex
    Participant

    Aaahhhh, this would explain a lot – I thought I was loosing my mind.

    I am glad you cleared this up, I can now give my head a rest from banging it off the desk.

    Much appreciated Dave 🙂

You must be logged in to reply to this topic.