ForEach with Multiple Variables - Powershell

Matt Leyland

$Res is the Result of an

Invoke-RestMethod

I currently have the following code.

$ObjectGUIDS = $Res.ObjectGUID

$AccountID = $Res.ID

$I = 0

ForEach ($ObjectGUID in $ObjectGUIDS) {

    Write-Host $ObjectGUID ($AccountID[$I])


    $I = $I +1
}

The Output

81d651d5-aeab-4a6f-a102-3e420b5bb630 5
b0f0283a-187f-4fab-9b3d-de98491c8283 6
b84c830a-9c01-49d2-adbd-d70d80199de1 7

I want to loop through the results which is fine if I was only wanted the result from one variable ie - ObjectGUID or AccountID. The code above is a botch and I don't feel its remotely correct however it does seem to work.

I did try ForEach within a ForEach but this will return each $ObjectGUID 3 Times for Each $AccountID

$ObjectGUIDS = $Res.ObjectGUID

$AccountIDS = $Res.ID

ForEach ($ObjectGUID in $ObjectGUIDS) {
    ForEach ($AccountID in $AccountIDS) {

    Write-Host $ObjectGUID ($AccountID)

    }
}

The Output

81d651d5-aeab-4a6f-a102-3e420b5bb630 5
81d651d5-aeab-4a6f-a102-3e420b5bb630 6
81d651d5-aeab-4a6f-a102-3e420b5bb630 7
b0f0283a-187f-4fab-9b3d-de98491c8283 5
b0f0283a-187f-4fab-9b3d-de98491c8283 6
b0f0283a-187f-4fab-9b3d-de98491c8283 7
b84c830a-9c01-49d2-adbd-d70d80199de1 5
b84c830a-9c01-49d2-adbd-d70d80199de1 6
b84c830a-9c01-49d2-adbd-d70d80199de1 7
Mark Wragg

I think you might be over complicating things by bringing the two properties of $Res in to separate variables.

I think this would work instead:

ForEach ($Result in $Res) {
    Write-Host $Result.ObjectGUID $Result.ID
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Powershell - Variables and foreach loop

Foreach multiple variables

Iterating multiple variables in ForEach

Powershell: dynamic variables within foreach

Powershell foreach regarding multiple collections

Pass variables to Foreach-Object Powershell 7

PowerShell/CLI: "Foreach" loop with multiple arrays

Powershell: apply multiple queries to a foreach on logs

PowerShell Add-Content with multiple variables to columns

powershell output multiple variables derived from processlist

How to check conditions of multiple variables in powershell

Powershell reading multiple variables from text file

Reading XML data using Powershell foreach and assign to the values Variables

How to access multiple variables outside foreach loop in php

Parallel.ForEach with Dictionary and multiple local variables in C#

PowerShell Foreach Loop Not Executing after multiple successful attempts

filter Foreach-Object results with multiple conditions in Powershell not working

How can I prevent from forming multiple tables in a Powershell foreach?

Foreach through XML document with Powershell returning multiple nodes

multiple PowerShell variables come up blank when in a line of output

Modify this Powershell custom input box to store multiple variables?

Returning multiple variables from within method in powershell class

foreach in foreach multiple dimensional

State Variables and ForEach Swift

PHP foreach if and use of || with variables

foreach loop with 3 variables

Multiple mutexes for multiple variables

Multiple if statements for multiple variables

Updating variables within variables in Powershell

TOP Ranking

HotTag

Archive