2

So I have an issue with my foreach loop, and I cant figure out what I am doing wrong.

The following is an example of my code:

 $objectarray ##( So this variable contains a list of lists ie name + address) ##

 Foreach ($object in $objectarray.name){


$objectid = $objectarray.Where({$_.name -eq "$object"}).id
$objectaddress = $objectarray.Where({$_.name -eq "$object"}).address

$objectprint = "$objectid" + ": " + "$objectaddress"

$objectprint
return 0

}

Now the problem is $objectarray has multiple lists inside it each with its name, id, address etc

But it ONLY prints the FIRST one, and I get ONLY the first 0 as return...despite the fact there are many of them

0

2 Answers 2

5

You're parsing through your object in a weird way. Why don't you use the pipeline to make this a little easier on yourself?

$ObjectArray | % { "$($_.id): $($_.address)"; 0 }

Everything will be put onto the success/output pipeline this way and can be captured (for example, if you put $var = before the expression)

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

2 Comments

because the ACTUAL $objectarray being parsed is MASSIVE with many many things to parse through.... And pipeline have memory optimization issues...Thank you very much for your recommendation though
Instead of $ObjectArray | % you can just use ForEach ($o in $ObjectArray) then.
1

I think its because you are returning 0 inside your for loop. Should put that outside the loop.

Foreach ($object in $objectarray.name){

    $objectid = $objectarray.Where({$_.name -eq "$object"}).id
    $objectaddress = $objectarray.Where({$_.name -eq "$object"}).address

    $objectprint = "$objectid" + ": " + "$objectaddress"

    $objectprint
}
return 0

6 Comments

Nope, doesnt work, Normally it doesnt make a difference either. Also every Object value is filled with data, none are empty or null, which is the reason why this is so baffling
you are returning 0
@A.Zia Sorry Ive updated the answer ( I believe this is the solution )
It captures the first entry's output which will be like: 100: Abc123 ... and a 0 as the next line (which is required to the other system i am feeding it to) ... but beyond the first entry no other output
Yeah but that return jumps you out of whatever function you are in, stopping the for loop.
|

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.