0

I have a (for me) strange result with a script. This is a summary of my script which returns the same result:

cls
$Init          = "AFFAIRES"
$ListeGroupes  = @() 
$NomGrScan     = "AFFAIRES"
$Grp0          = New-Object psobject
$Grp0          | Add-Member -Name "Nom"  -MemberType NoteProperty -value $Init
$Grp0          | Add-Member -Name "Scan" -MemberType NoteProperty -value "N"
$ListeGroupes += $Grp0 
$LGroupes1     = @()
$LGroupes1    += $ListeGroupes
write-host "Grp0 : $($Grp0.Nom) - $($Grp0.scan)"
write-host "ListeGroupes : $($ListeGroupes.Nom) - $($ListeGroupes.scan)"
write-host "LGroupes1 : $($LGroupes1.Nom) - $($LGroupes1.scan)"
($LGroupes1 | where {$_.nom -eq "$NomGrScan"}).scan = "O"
write-host "============================================="
write-host "Grp0 : $($Grp0.Nom) - $($Grp0.scan)"
write-host "ListeGroupes : $($ListeGroupes.Nom) - $($ListeGroupes.scan)"
write-host "LGroupes1 : $($LGroupes1.Nom) - $($LGroupes1.scan)"

When I run it on my computer I get:

Grp0 : AFFAIRES - N
ListeGroupes : AFFAIRES - N
LGroupes1 : AFFAIRES - N
=============================================
Grp0 : AFFAIRES - O
ListeGroupes : AFFAIRES - O
LGroupes1 : AFFAIRES - O

On line 15 of the script I only change the value of $LGroupe1. Why does it also change the values of $ListeGroupe and $Grp0?

I don't understand why I don't get:

Grp0 : AFFAIRES - N
ListeGroupes : AFFAIRES - N
LGroupes1 : AFFAIRES - N
=============================================
Grp0 : AFFAIRES - N
ListeGroupes : AFFAIRES - N
LGroupes1 : AFFAIRES - O

2 Answers 2

1

In Powershell, objects (like the value in $Grp0) are treated as references. So, assigning a varible to an existing object gets you - not a new object - but another reference to the same object.

$a = New-Object PSObject -Property @{ prop1='a' ; prop2='b' }
$b = $a

$b points to the same object as $a. Changes to one affect the other.

One possible way to get around this is to use the Copy() method on PSObject.

$a = New-Object PSObject -Property @{ prop1='a' ; prop2='b' }
$b = $a.PSObject.Copy()

Now, $a and $b point to different objects and changes to one will not affect the other.

Note that Copy() only does a shallow copy.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help. Now I understand better.
0

I came to try your code :

$a = New-Object PSObject -Property @{ prop1='a' ; prop2='b' }
$b = $a.Copy()

and i have this (sorry it's in french) :

Échec lors de l’appel de la méthode, car [System.Management.Automation.PSCustomObject] ne contient pas de méthode nommée « Copy ».
_Failed to call the method, because [System.Management.Automation.PSCustomObject] does not contain a method named "Copy"_
Au caractère Ligne:2 : 1
+ $b = $a.Copy()
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (Copy:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Where is my mistake ? thank you

1 Comment

Ooop! forgot the important qualifier on the Copy(). That line should be $b = $a.PSObject.Copy() Answer corrected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.