0

suppose i have the following pscutomobject array script

$pscoArray = @()

foreach($server in $Servers) {
    $pscoArray += [PSCustomObject]@{
        Server = $server
    }
}                   

$iteration = 0

how do i loop through the resulting $pscoArray and add to it a new member called 'iteration' and increment that based on number of objects that already exist in the array?

i.e.

$pscoArray = $pscoArray | %{ Add-Member -MemberType NoteProperty -Name 'iteration' -Value $iteration; 'iteration' = $iteration++}

ultimate output should look like this, assuming there are 3 servers in this example

iteration Server
------    ------
1         server1
2         server2
3         server3

so iteration has to be added as a member at position 0, at the beginning of the pscustomobject, although i dont know how would i do that either

p.s. i know i can just add iteration directly in the forloop, but i need this for a different purpose.

2
  • 1
    What exactly are you trying to achieve? The code looks like you could do with a simple string array as the PsCustomObjects only have one property? If you want to add new properties to the objects while iterating the array, have a look at Add-Member Commented Aug 21, 2019 at 19:49
  • @Theo ya so im trying to add a new member, called iteration. but it should also count however many objects already exist in the pscoarray and increment the new iteration member that was added. this has to be done OUTSIDE the foreach loop because as i said, i have some other purposes for this so i can just add the mmber inside the foreachloop Commented Aug 21, 2019 at 19:53

1 Answer 1

2

If this is the code that generates your array

$Servers = 'svr1', 'svr2', 'svr3'
$pscoArray = foreach($server in $Servers) {
    [PSCustomObject]@{
        Server = $server
    }
}

Then the simplest way of doing this may be by using a for(..) loop

# add a new property called "iteration" to each member of the array
for ($i = 0; $i -lt $pscoArray.Count; $i++) {
    $pscoArray[$i] | Add-Member -MemberType NoteProperty -Name 'iteration' -Value ($i + 1)
}

$pscoArray

Output:

Server iteration
------ ---------
svr1           1
svr2           2
svr3           3


Update

If you want to get the iteration property listed first, you could do:

# add a new property called "iteration" as first property to each member of the array
for ($i = 0; $i -lt $pscoArray.Count; $i++) {
    $pscoArray[$i] = $pscoArray[$i] | Select-Object @{Name = 'iteration'; Expression = {$i + 1}}, *
}

$pscoArray

Output:

iteration Server
--------- ------
        1 svr1  
        2 svr2  
        3 svr3
Sign up to request clarification or add additional context in comments.

10 Comments

question: can the iteration be rearraneged to be at the beginning of the pscoArray? so this code is perfect, i just need iteration to be the first column :)
@Cataster I have added a new code suggestion so each object in the array will be overwritten by a new object that has the iteration property in first, combined with everything the object already had in it (that is the asteriks * at the end of the Select-Object)
@Cataster Glad to have helped!
@Cataster In that case do $pscoArray | Where-Object { $_.Server -ne 'svr2' }
@Cataster No, I don't know why some people do that. The question to me is perfectly legit and I see no reason for downvoting whatsoever. I evened the score with a +1. Cheers!
|

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.