Hash Table only contains last result?

Welcome Forums General PowerShell Q&A Hash Table only contains last result?

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 10 months ago.

  • Author
    Posts
  • #24338

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 502
    Helping Hand
    Rank: Major Contributor

    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. 🙂

The topic ‘Hash Table only contains last result?’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort