[英]ForEach with Multiple Variables - Powershell
$Res is the Result of an $ Res是结果
Invoke-RestMethod
调用-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. 我想遍历结果,如果我只想要一个变量(即-ObjectGUID或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 我确实在ForEach中尝试了ForEach,但这将为每个$ AccountID返回每个$ ObjectGUID 3次
$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
I think you might be over complicating things by bringing the two properties of $Res
in to separate variables. 我认为通过将
$Res
的两个属性带到单独的变量中可能会使事情复杂化。
I think this would work instead: 我认为这可以代替:
ForEach ($Result in $Res) {
Write-Host $Result.ObjectGUID $Result.ID
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.