Search code examples
powershellpowershell-5.1

Function only diplays properties once


I have a function that for some reason, just seems to only display $Properties once, no matter how many users are passed to the script. Tried to throw in just the $user passed into the script to display but, that too only displays once. Not too sure why it's doing this as I have exhausted ideas on what it may be:

Function Set-DRAUserProperty {
    Param (
        
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [string[]]$UserName,

        $Company,
        $Department,
        $Name,
        $O, 
        $PhysicalDeliveryOfficeName,
        $TelephoneNumber,
        $Title
    )
    DynamicParam
    {
        if ($UserName.Split(',').Count -le 1) {
            $displayNameAttribute = New-Object System.Management.Automation.ParameterAttribute
            $displayNameAttribute.Mandatory = $false
            $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($displayNameAttribute)
            $displayNameParam = New-Object System.Management.Automation.RuntimeDefinedParameter('DisplayName', [string], $attributeCollection)
            $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add('DisplayName', $displayNameParam)
            $paramDictionary
       }
    }
    Begin 
    {
        $OrgBoxOu   = "*OU=xxxxxxxxx"
        $Parameters = $PSBoundParameters
        $Properties = @{}
    }
    Process 
    {
        foreach ($User in $UserName) 
        {   
            try {
                $SelectedUser = Get-ADUser -Identity $User -Properties DisplayName
                    if ($SelectedUser) {
                        $Parameters.GetEnumerator() | Where-Object -FilterScript {$_.Key -ne 'UserName'} | 
                            ForEach-Object -Process `
                            {
                                if ($_.Key -eq 'DisplayName' -and $SelectedUser.DistinguishedName -like $OrgBoxOu) {
                                    if ('FirstNamePreferred' -in $Properties.Keys) {
                                        Continue
                                    }
                                    else {
                                        $Properties.Add($_.Key, $_.Value)
                                        $Properties.Add('FirstNamePreferred', $_.Value) 
                                    }
                                }
                                else {
                                    if ($_.Key -in $Properties.Keys) { 
                                        Continue
                                    }
                                    else {
                                        $Properties.Add($_.Key, $_.Value) 
                                    }
                                }
                            }

                        $Properties.GetEnumerator()
                        #Set-DRAUser -Identifier $SelectedUser.DistinguishedName -Properties $Properties @DRA_Server_Splat -ErrorAction Stop
                    }

                    else {
                        Write-Host -Object "No valid member selected!"
                    }
            }
            catch {
                Write-Host -Object "ERROR: $($_.Exception.Message)" -ForegroundColor Red -BackgroundColor Black
                continue
            }

        }

    }
    End { }
}

When running the function,

  • Set-DRAUserProperty -UserName Abe,Abe -Company ssc

. . . the output is only displayed once:

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Company                        ssc  

What i'm tring to achieve is having the values be splatted onto another cmdlet, but it won't work as long as my $Properties hashtable only displays once. So expected output would be it display the hashtable for each user that's been passed to the function:

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Company                        ssc  

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Company                        ssc  

Just looking for some fresh eyes that can point me in the right direction and/or, shed some light on what might be happening (wrong). Not sure what could be causing the issue so can't pin-point it to a specific code section.

Please edumecate me:)


Solution

  • Solution was to define the hashtable inside the foreach loop instead of inside the begin { ... } block:

    foreach ($User in $UserName) 
    {
        $Properties = @{}
        ....
        ....
    }
    

    But if you want to understand why it was not working before?, this is your hint:

    if ($_.Key -in $Properties.Keys) { 
        '{0} is in $Properties.Keys :)' -f $_.Key
        Continue
    }
    

    Which yields:

    Name                           Value
    ----                           -----
    Company                        ssc
    Company is in $Properties.Keys :)
    Company is in $Properties.Keys :)
    

    And this only happens because $Properties is the same on each iteration (because it was defined in the begin { ... } block).