Hash Table only contains last result?

This topic contains 3 replies, has 3 voices, and was last updated by  Rob Simmers 2 years, 4 months ago.

  • Author
    Posts
  • #24338

    Karson Van Meeteren
    Participant

    I'm iterating through an clean array of user objects and trying to assign their AD properties to a hash table. However, only the last object in that array shows up in the final hash table, so I assume (eek!) that my foreach is overwriting the hash table each time it iterates through.

    I'm a hash table n00b, so any advice would be great on where I'm going wrong here would be mucho appreciated!

    Thanks for all you do here, ladies and gents!

    $userPSObjectCollection = @()
    foreach ($user in $finalarrayUsers)
    {
    	$userInfo = Get-ADUser $user -Properties Name, EmailAddress
    	$userInfoHash = [ordered]@{
    		Name = $userInfo.name
    		EmailAddress = $userInfo.emailaddress
    		}
    }
    
    $userPSObject = New-Object -TypeName PSObject -Property $userInfoHash
    $userPSObject
    $userPSObjectCollection += $userPSObject
    $userPSObjectCollection | ogv
    
  • #24339

    Don Jones
    Keymaster

    You're overwriting $userInfoHash each time through the loop, and you're only creating an object after the loop completes. Your object creation code should be inside the ForEach loop, not at the end of it.

  • #24340

    Karson Van Meeteren
    Participant

    You're overwriting $userInfoHash each time through the loop, and you're only creating an object after the loop completes. Your object creation code should be inside the ForEach loop, not at the end of it.

    Doh! Thanks, Don! Here's the working snippet 🙂

    $userPSObjectCollection = @()
    foreach ($user in $finalarrayUsers)
    {
    	$userInfo = Get-ADUser $user -Properties Name, EmailAddress
    	$userInfoHash = [ordered]@{
    		Name = $userInfo.name
    		EmailAddress = $userInfo.emailaddress
    	}
    	$userPSObject = New-Object -TypeName PSObject -Property $userInfoHash
    	$userPSObject
    	$userPSObjectCollection += $userPSObject
    }
    
    
    $userPSObjectCollection | ogv
    
  • #24342

    Rob Simmers
    Participant

    For what you are doing currently, this is the same thing:

    $userPSObjectCollection = @()
    foreach ($user in $finalarrayUsers)
    {
    	$userInfo = Get-ADUser $user -Properties Name, EmailAddress | Select Name, EmailAddress
    	$userPSObjectCollection += $userInfo
    }
    $userPSObjectCollection  | ogv
    

    If you were compiling information from multiple sources, you could use a method to generate a custom object, but since you are just getting information from a single cmdlet you can just use Select-Object, which also generates a new custom object with the properties you specify. You can also do this:

    $userPSObjectCollection = @()
    # Assign the New-Object result to the variable using the for loop
    $userPSObjectCollection = foreach ($user in $finalarrayUsers){
    	$userInfo = Get-ADUser $user -Properties Name, EmailAddress
        $os = Get-CimInstance -ClassName Win32_OperatingSystem
    	$userInfoHash = [ordered]@{
    		Name = $userInfo.name
    		EmailAddress = $userInfo.emailaddress
            RegisteredUser = $os.RegisteredUser
    	}
    	New-Object -TypeName PSObject -Property $userInfoHash
    }
     
     
    $userPSObjectCollection | ogv
    

    or

    $userPSObjectCollection = @()
    $userPSObjectCollection = foreach ($user in $finalarrayUsers){
        $os = Get-CimInstance -ClassName Win32_OperatingSystem	
        #Calculated property and leverage Select-Object to create the custom object
        Get-ADUser $user -Properties Name, EmailAddress | Select Name, EmailAddress, @{Label="RegisteredUser";Expression={$os.RegisteredUser}}
    }
     
     
    $userPSObjectCollection | ogv
    

    Just some options for current or future projects. 🙂

You must be logged in to reply to this topic.