Why alias does not work?

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!

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.

You are right! It WORKS! But looks weird.

Thanks