1

I have a powershell script running steps such as

$stuffFromAPI = $resultsFromAPI.inputs.Stuff | ConvertFrom-Json
$objectFromAPIStuff = $stuffFromAPI.item[0] #ipAddress = 127.0.0.1 
$newStuff = @()
$tempStuff = $null
$condition = "yup"
$IPAddress = "10.0.0.1"
if($condtion -eq "yup")
{
    $tempStuff = $objectFromAPIStuff
    $tempStuff.ipAddress = $IPAddress
    $newBindings+=$tempStuff
}

Write-Host $tempStuff.ipAddress
Write-Host $objectFromAPIStuff.ipAddress
Write-Host $stuffFromAPI.item[0].ipAddress

The output will be

10.0.0.1
10.0.0.1
10.0.0.1

why is $objectFromAPIStuff and even deeper $stuffFromAPI.item[0] inheriting the changed values of $tempStuff

There seems to be some link between them in memory. I have closed powershell ISE and reopened it, and I have even restarted my computer thinking something was just cached weird in powershell ISE

Nick
  • 179
  • 4
  • 13
  • 2
    This is expected behavior. `$tempStuff`, `$objectFromAPIStuff` and `$stuffFromAPI.item[0]` all contain a reference to the same object in memory – Mathias R. Jessen Jul 17 '23 at 18:14
  • How do I separate them so I can edit them individually? – Nick Jul 17 '23 at 18:22
  • 1
    The linked duplicate discusses the underlying concepts as well as (invariably limited) options for cloning objects (making independent copies). – mklement0 Jul 17 '23 at 19:11

0 Answers0