Can't get ValueFromPipelineByPropertyName to work with Get-ADComputer

Hi!

I am trying to create a CMDlet that will get input from Get-ADuser or Get-ADComputer but i can't get it to work with the ValueFromPipelineByPropertyName property.

The script below only outputs the name of the user or computer but it does not work with Get-ADuser or Get-ADcomputer, it does work if i create my own object as you can see below

function Just-a-Test
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
                   [Alias("Name")]
        $TheName
    )

    Begin
    {
    }
    Process
    {
    Write-Host $thename
    }
    End
    {
    }
}


$obj = New-Object psobject -Property @{name = "test"}  


# Output: Test
$obj | Just-a-Test 

#Output: Just empty rows, Why oh why?
Get-ADComputer -Filter * | Just-a-Test 

Any idea why?

Thanks!

January 10th, 2014 1:28pm

Try below:

C:\> $computer = Get-ADComputer -Identity Server01
C:\> function Just-a-Test
>>> {
>>>     [CmdletBinding()]
>>>     [OutputType([int])]
>>>     Param
>>>     (
>>>         # Param1 help description
>>>         [Parameter(Mandatory=$true,
>>>                    ValueFromPipelineByPropertyName=$true,
>>>                    Position=0)]
>>>                    [Alias("ComputerName")]
>>>         $Name
>>>     )
>>>
>>>     Begin
>>>     {
>>>     }
>>>     Process
>>>     {
>>>     Write-Host $name
>>>     }
>>>     End
>>>     {
>>>     }
>>> }
C:\> $computer | Just-a-Test
Server01
C:\>

You need to set the parameter name exactly the same name of the property on the object being piped.

Make sense ?

Free Windows Admin Tool Kit Click here and download it now
January 10th, 2014 1:37pm

Ah okey! Now it works!

Why does my first example works then? I pipe in the Name property but output $TheName variable?

Thanks for the help!

January 10th, 2014 1:47pm

I'm not sure why this is, but I've come across similar issues trying to pipe the AD cmdlets into functions by property name. If I remember correctly, I was able to get it working like this:

Get-ADComputer -Filter * |
Select-Object -Property * |
Just-a-Test 

It looks stupid.  It shouldn't make any difference, but for some reason, it does.

  • Marked as answer by Samus-Aran Friday, January 10, 2014 1:21 PM
Free Windows Admin Tool Kit Click here and download it now
January 10th, 2014 4:12pm

Yes you are remembering it correctly!

I can work with this.

Thanks!

January 10th, 2014 4:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics