Powershell. Using Add-member on elements in an array
I’m making a new Powershell inventory script. I already have one, but I’m fiddling with the arrays to see if I can make the code a little prettier. I can make variables that have custom members with the add-member command. An example:
That’s easy enough, but I want to set it in an array named $hosts. To replicate the last 4 lines of the preceding code:
So now how do I do that? I tried this:
I figured that would run the Add-Member command on each element in the array. It didn’t produce any errors, but it didn’t add an Online member to the elements.
I found a more complicated way that added variables with custom members to an array”
This works. The values of $computer1 and $computer2 are fed into $hosts[0] and $hosts[1], custom members and all. But I need to iterate this n times, and I don’t know the value of n. So I made a $placeholder variable that would change within a loop.
The following code uses a for loop to change the value of $placeholder 10 times and add those values to $hosts 10 times.
This is the part that I don’t understand. In the middle of the loop, everything matches up. This is the output I see:
And so on. $place gets updated, and $hosts get a new entry.
But as it exits out of the loop, all elements of $hosts get the same value. This is the final output:
WTF? Each iteration of the loop adds a new element to the array. But then each element takes on the current value of $place. I want each element to get set once and stay that way.
Is each element a kind of pointer? Forgive me if I have the wrong terminology. When I write “$hosts += $place” does that make the new element point to $place? How do I assign to it the value of $place and not $place itself? Any assistance would be greatly appreciated.
I’m making a new Powershell inventory script. I already have one, but I’m fiddling with the arrays to see if I can make the code a little prettier. I can make variables that have custom members with the add-member command. An example:
Code:
$computer1 = new-object psobject
$computer1 | add-member –membertype NoteProperty –name Online –value NotSet
$computer1 | add-member –membertype NoteProperty –name HostName –value NotSet
$computer1 | add-member –membertype NoteProperty –name Serial –value NotSet
$computer2 = new-object psobject
$computer2 | add-member –membertype NoteProperty –name Online –value NotSet
$computer2 | add-member –membertype NoteProperty –name HostName –value NotSet
$computer2 | add-member –membertype NoteProperty –name Serial –value NotSet
$computer1.HostName = “PC15”
$computer1.Serial = “43251”
$computer2.Online = “N”
$computer2.Serial = “12345”
That’s easy enough, but I want to set it in an array named $hosts. To replicate the last 4 lines of the preceding code:
Code:
$hosts[0].HostName = “PC15”
$hosts[0].Serial = “43251”
$hosts[1].Online = “N”
$hosts[1].Serial = “12345”
So now how do I do that? I tried this:
Code:
$hosts=@([psobject])
foreach ($i in $hosts){
$i | add-member –membertype NoteValue –name Online –value NotSet
}
I figured that would run the Add-Member command on each element in the array. It didn’t produce any errors, but it didn’t add an Online member to the elements.
I found a more complicated way that added variables with custom members to an array”
Code:
$computer1 = new-object psobject
$computer1 | add-member –membertype NoteProperty –name Online –value NotSet
$computer1 | add-member –membertype NoteProperty –name HostName –value NotSet
$computer1 | add-member –membertype NoteProperty –name Serial –value NotSet
$computer2 = new-object psobject
$computer2 | add-member –membertype NoteProperty –name Online –value NotSet
$computer2 | add-member –membertype NoteProperty –name HostName –value NotSet
$computer2 | add-member –membertype NoteProperty –name Serial –value NotSet
$hosts = @([psobject])
$hosts += $computer1
$hosts += $computer2
$hosts[0].HostName = “PC15”
$hosts[0].Serial = “43251”
$hosts[1].Online = “N”
$hosts[1].Serial = “12345”
This works. The values of $computer1 and $computer2 are fed into $hosts[0] and $hosts[1], custom members and all. But I need to iterate this n times, and I don’t know the value of n. So I made a $placeholder variable that would change within a loop.
The following code uses a for loop to change the value of $placeholder 10 times and add those values to $hosts 10 times.
Code:
Clear
$hosts=@([psobject])
$place = new-object psobject
$place | add-member –membertype NoteProperty –name Number –Value NotSet
$place | add-member –membertype NoteProperty –name HostName –Value NotSet
For($j=0;$j –le 9;$j++){
$place.Number = $j
$place.HostName = “Computer $j”
$hosts += $place
“-------------“
“Iteration $j. Printing current values for place and hosts”
“Place”
$place
“Hosts”
$hosts[$j]
}
“Completed for loop.”
“Value of Hosts array”
$hosts
This is the part that I don’t understand. In the middle of the loop, everything matches up. This is the output I see:
Code:
-------------
Iteration 0. Printing current values for place and hosts
Place
0 Computer 0
Hosts
0 Computer 0
-------------
Iteration 1. Printing current values for place and hosts
Place
1 Computer 1
Hosts
1 Computer 1
And so on. $place gets updated, and $hosts get a new entry.
But as it exits out of the loop, all elements of $hosts get the same value. This is the final output:
Code:
Value of Hosts array
5 Computer 5
5 Computer 5
5 Computer 5
5 Computer 5
5 Computer 5
5 Computer 5
WTF? Each iteration of the loop adds a new element to the array. But then each element takes on the current value of $place. I want each element to get set once and stay that way.
Is each element a kind of pointer? Forgive me if I have the wrong terminology. When I write “$hosts += $place” does that make the new element point to $place? How do I assign to it the value of $place and not $place itself? Any assistance would be greatly appreciated.