Author Posts

April 16, 2015 at 5:18 am

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

April 16, 2015 at 5:19 am

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.

April 16, 2015 at 5:22 am

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

April 16, 2015 at 5:54 am

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