While writing a script last week, I came across on odd behavior while tracking down a bug. I've created the following example code which represents the issue. The main question I have is: Why does the $Report variable get 2 extra objects in it when I DON'T explicitly assign the return value from the 'Set-CommonValues' function.
Example Code - The code below works as intended, I get two objects in the $Report variable after running it.
Function Set-CommonValues
{
param
(
[Parameter(Mandatory)]
[object] $UserReportObj
)
$UserReportObj | Add-Member -Type NoteProperty -Name "Set Value 1" -Value $True -Force
$UserReportObj | Add-Member -Type NoteProperty -Name "Set Value 2" -Value $True -Force
Return $UserReportObj
}
Function Process-UserObject
{
param
(
[Parameter(Mandatory)]
[object] $UserReportObj
)
$UserReportObj | Add-Member -Type NoteProperty -Name "Ad Domain" -Value "contoso.com" -Force
# Set-CommonValues -UserReportObj $UserReportObj
$UserReportObj = Set-CommonValues -UserReportObj $UserReportObj
return $UserReportObj
}
$Names = ("John Doe", "Jane Doe")
$Report = @()
foreach($Name in $Names)
{
$UserObj = New-Object -TypeName PSObject
$UserObj | Add-Member -Type NoteProperty -Name "Name" -Value $Name -Force
$UserObj = Process-UserObject -UserReportObj $UserObj
$Report+= $UserObj
}
return $report
If I run the script as shown above, it works as intended. I get two objects in the $Report variable. However, if I uncomment the line in the function 'Process-UserObject' and then comment out the line below it so it looks like the code below, I get 4 objects in the $Reports variable. My Expectation would be that when I don't explicitly assign the return value from the 'Set-CommonValues' function, the properties set by that function simply aren't included in the objects I later assign to $Reports, OR, if powershell treats objects as references, me explicitly assigning the return value isn't even needed. The Add-Member command modifies the passed object and I don't need to explicitly assign the object back to the $UserObj variable. Instead, I somehow get 2 extra objects in the $Report variable. Can someone explain to me why this occurs?
Example Code - Don't understand why the $Report variable get's 4 objects in the below example.
Function Set-CommonValues
{
param
(
[Parameter(Mandatory)]
[object] $UserReportObj
)
$UserReportObj | Add-Member -Type NoteProperty -Name "Set Value 1" -Value $True -Force
$UserReportObj | Add-Member -Type NoteProperty -Name "Set Value 2" -Value $True -Force
Return $UserReportObj
}
Function Process-UserObject
{
param
(
[Parameter(Mandatory)]
[object] $UserReportObj
)
$UserReportObj | Add-Member -Type NoteProperty -Name "Ad Domain" -Value "contoso.com" -Force
Set-CommonValues -UserReportObj $UserReportObj
# $UserReportObj = Set-CommonValues -UserReportObj $UserReportObj
return $UserReportObj
}
$Names = ("John Doe", "Jane Doe")
$Report = @()
foreach($Name in $Names)
{
$UserObj = New-Object -TypeName PSObject
$UserObj | Add-Member -Type NoteProperty -Name "Name" -Value $Name -Force
$UserObj = Process-UserObject -UserReportObj $UserObj
$Report+= $UserObj
}
return $report