Function Alias Question

This topic contains 2 replies, has 2 voices, and was last updated by  Alex 1 year, 3 months ago.

  • Author
    Posts
  • #38818

    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

    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

    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.