I read this article in Powershell online magazine and I am having a hard time getting my mind wrapped around it as to what it means.
It gives the following example code:
$groups = 'Group1', 'Group2' $users = 'User1', 'User2' $objectCollection=@() $object = New-Object PSObject Add-Member -InputObject $object -MemberType NoteProperty -Name Group -Value "" Add-Member -InputObject $object -MemberType NoteProperty -Name User -Value "" $groups | ForEach-Object { $groupCurrent = $_ $users | ForEach-Object { $userCurrent = $_ $object.Group = $groupCurrent $object.User = $userCurrent $objectCollection += $object } } $objectCollection
It shows the output as this:
Group User ----- ---- Group2 User2 Group2 User2 Group2 User2 Group2 User2
It then explains why we are getting this output. The is the paragraph I am having a problem understanding as well as the link to the article.
We assign the information to the object and add it to the collection, but we still use the same object. When we added it to the collection we added just a 'reference' to the object, not the object itself. What we ended up with was just four 'shortcuts' to the same object. From
I don’t understand they mean by 'reference to the object and not the object itself. When you are using the using the statement,
$object.Group = $groupCurrent and $object.User = $userCurrent, are you not adding those values to that object? I don’t understand what they mean by ‘reference’.