PowerShell function ValueFromPipelineByPropertyName not using alias parameter name

前端 未结 2 1650
离开以前
离开以前 2020-12-11 20:47

Trying to create a function that takes objects on the pipeline using the alias property. I\'m not sure where this is going wrong.

Example of the process:

<         


        
2条回答
  •  遥遥无期
    2020-12-11 21:21

    From what I've been able to determine, it isn't technically the AD cmdlets that are to blame, but the types in the Microsoft.ActiveDirectory.Management namespace--in this case, ADUser. The properties on ADUser are ultimately all just stored in a private SortedDictionary and fetched through get accessors, which might explain why it doesn't work quite as expected.

    As alluded to by Colyn1337 in a previous comment, ADUser doesn't contain a property (or key) named either sn or LastName by default, so you'd need to either include an alias of Surname on your LastName parameter or select sn in your Get-ADUser call:

    Get-ADUser -Filter {sn -eq 'Adkison'} -Properties sn | Get-Name
    

    That still won't work, but from there you can just pipe to Select-Object before piping to your function:

    Get-ADUser -Filter {sn -eq 'Adkison'} -Properties sn | Select * | Get-Name
    

    Of course, you could also just select the specific properties you need instead of * in Select-Object. I assume this works because it resolves the ADUser dictionary into a PSCustomObject with concrete properties. Once resolved, they will match aliases as well as the actual parameter names.

提交回复
热议问题