r/PowerShell•
jsproat

Need help with ValueFromPipelineByPropertyName

I'm having trouble getting ValueFromPipelineByPropertyName to work. I want to pass my function an array of string (usernames), and also have it accept hashtables within that array, which define username and email address. I'm looking at how Select-Object's -Property parameter accepts an array which can hold both strings and script blocks.

My script:

[CmdletBinding()]
param (
    [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
    [Alias('AccountName', 'SamAccountName')]
    [string[]] $UserName
)

process {
    write-verbose 'Create-UserCertificate: process {} top'
    foreach ($user in $UserName) { write-output $user }
    write-verbose 'Create-UserCertificate: process {} bottom'
}

What I expect to see:

PS> .\Create-UserCertificate.ps1 biff, @{username='testblaz'; emailaddress='[email protected]'}, blaz -verbose
VERBOSE: Create-UserCertificate: process {} top
biff
testblaz    <-- note this output
blaz
VERBOSE: Create-UserCertificate: process {} top

What I actually see:

PS> .\Create-UserCertificate.ps1 biff, @{username='testblaz'; emailaddress='[email protected]'}, blaz -verbose
VERBOSE: Create-UserCertificate: process {} top
biff
System.Collections.Hashtable    <-- *BOOM*
blaz
VERBOSE: Create-UserCertificate: process {} top

I know I can do what I need to if I explicitly test for the classname Hashtable, and then extract the username and emailaddress, but I thought that ValueFromPipelineByPropertyName was supposed to do some of that automatically.

If it helps, I'm stuck with PowerShell v2.0 for the time being. Wish I could take advantage of 3.0+, but I can't right now.

Posted May 6, 2015, 4:01 PM UTC

56
Loading comments…