0

The below function is supposed to add the the passed parameters, and values, to a hashtable for later use when setting the actual properties but. . . for a reason I can't understand, the hashtable is only displayed once, with just one set of properties.

I'm trying to have it splat all the values passed for each user, but it only does it for one.

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

        $Company,
        $Department,
        $DisplayName,
        $Name,
        $O, 
        $PhysicalDeliveryOfficeName,
        $TelephoneNumber,
        $Title
    )
    Begin 
    {
        $OrgBoxOu   = "*OU=XX, DC=XX"
        $Parameters = $PSBoundParameters
        $Properties = @{}
    }
    Process 
    {
        foreach ($User in $UserName) 
        {    
             try {
                $SelectedUser = $User
                    if ($SelectedUser) {
                        $Parameters.GetEnumerator() | Where-Object Key -ne 'UserName' | 
                            ForEach-Object -Process `
                            {

                                if ($_.Key -eq 'DisplayName' -and $SelectedUser.DistinguishedName -notlike $OrgBoxOu) {
                                    $Properties.Add('FirstNamePreferred', $_.Value) 
                                }

                                $Properties.Add($_.Key, $_.Value) 

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

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

    }
    End { }
}

Using the command line arguments: Change-DRAUserProperty -UserName Abe, Abe2, Abe3 -Company SSC -Department USSF

. . .only displays:

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Department                     USSF                                                                                                                                                                                                                              
Company                        SSC     

Instead of one per user passed (what the results should be):

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Department                     USSF                                                                                                                                                                                                                              
Company                        SSC     
Name                           Value   
                                                                                                                                                                                                                          
----                           -----                                                                                                                                                                                                                             
Department                     USSF                                                                                                                                                                                                                              
Company                        SSC     

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Department                     USSF                                                                                                                                                                                                                              
Company                        SSC     

No too sure what I may be doing wrong, may I have someone else take a look over and point out my mistakes?

Am I going crazy guys?

Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • 1
    Have you tried single-stepping through the code using a debugger? I can recommend VSCode with PowerShell extension for this purpose. – zett42 Nov 05 '21 at 15:17
  • @Zett42, tried it man. No luck, was using ISE at first but, switched ti VSC. Anything look out of order for you? – Abraham Zinala Nov 05 '21 at 15:23
  • 2
    My immediate guess is that on the second iteration over `$Username`, the call to `$Properties.Add` fails because the key already exists in the dictionary - and your error handling then deceives you because of the way you've formatted the `Write-Host` string. Try changing it to `Write-Host "ERROR: $(...)"` so that it always writes _something_ (even if the underlying exception message cannot be resolved) – Mathias R. Jessen Nov 05 '21 at 15:28
  • @Mathias. Ohh man, I believe that's it! I also had a typo: `$($_.Exception.Messge)` should be `$($_.Exception.Message)`. Message was spelled incorrectly so no error was being shown. Thank you for that! – Abraham Zinala Nov 05 '21 at 15:31
  • 1
    Mathias is right: `Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: 'Company' Key being added: 'Company'"` – Santiago Squarzon Nov 05 '21 at 15:33
  • This old age is getting to me... needed some fresh young eyes! Thanks guys! :) – Abraham Zinala Nov 05 '21 at 15:34
  • 1
    @AbrahamZinala you're welcome, good luck with future lasik surgery ^_^ – Mathias R. Jessen Nov 05 '21 at 15:35
  • It's hard being 25. . .you know what they say, 25 is the new 80! haha hope all yalls pillows are cold on both sides tonight!;) Thanks!! – Abraham Zinala Nov 05 '21 at 15:37

0 Answers0