How to pass new iteration value into array in a loop every nth element?

I am trying to say for every 7/3 or 2 $someArray values ($([math]::round($someArray.count/$bodyArray.count))), pass a new schedule to the restmethod, but for the life of me i cannot figure out how to do that…

$someArray = 'value1','value2','value3','value4','value5','value6','value7'

$bodyArray = @()

$i = 0

foreach($val in $someArray)
{
    $bodyArray += 'schedule1=$1','schedule2=$i','schedule3=$i'

    Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$([math]::round($someArray.count/$bodyArray.count))] | ConvertTo-Json -Depth 100) -ContentType "application/json"

    $i++
    $bodyArray = @() #reset the array
}

essentially, iteration1 will be:

$bodyArray[schedule1]

iteration2 will be $bodyArray[schedule1]

iteration3 would be

$bodyArray[schedule2]

and so forth

this is also posted here for reference

If I got you right you could use a for loop and adjust the repetition interval to your needs. Something like this:

$someArray = 'value1','value2','value3','value4','value5','value6','value7'
$index = 0
for($index ; $index -lt $someArray.Count ; ($Index = $index + 3)) {
    $someArray[$index]
}

[quote quote=211200]If I got you right you could use a for loop and adjust the repetition interval to your needs. Something like this:

$someArray = 'value1','value2','value3','value4','value5','value6','value7'
$index = 0
for($index ; $index -lt $someArray.Count ; ($Index = $index + 3)) {
$someArray[$index]
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

i think there is a misunderstanding. what i am trying to say is:

pseudocode:

$frequency=0

foreach ($val in $someArray)

{

$bodyArray …

for every 2 values (i.e. after 2nd element, after 4th element, etc. in $someArray) {$frequency++}

invoke-restmethod … -Body ($bodyArray[$frequency])

}

Sorry, I cannot follow. Maybe it’s too late for me. :wink:

If I understand correctly, you are looking for something like this?

$someArray = 'value1','value2','value3','value4','value5','value6','value7'

$bodyArray = @()
$i = 0

$someArray | ForEach-Object {
    If (-Not($bodyArray.Count%2)) {
        #Increment on Even
        $i++
    }
    $bodyArray += "schedule1=$i"
}

$bodyArray

#Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$([math]::round($someArray.count/$bodyArray.count))] | ConvertTo-Json -Depth 100) -ContentType "application/json"

Results:

schedule1=1
schedule1=1
schedule1=2
schedule1=2
schedule1=3
schedule1=3
schedule1=4

Don’t worry Curtis. I didn’t understand this either. Just like other much smarter people than me … :wink:

https://social.technet.microsoft.com/Forums/en-US/bfc5a5ed-0702-4ad8-9d23-53a4e0069138/how-to-increment-in-a-loop-every-n-items?forum=winserverpowershell

https://stackoverflow.com/questions/60749227/how-to-pass-new-iteration-value-into-array-in-a-loop-every-nth-element

Or if you need to be able to adjust your increment frequency

$someArray = 'value1','value2','value3','value4','value5','value6','value7'
$frequency = 3

$bodyArray = @()
$current = 0
$i = 1

$someArray | ForEach-Object {
    If ($current -eq $frequency) {
        $current = 0
        $i++
    }
    $bodyArray += "schedule1=$i"
    $current++
}

$bodyArray

#Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$([math]::round($someArray.count/$bodyArray.count))] | ConvertTo-Json -Depth 100) -ContentType "application/json"

Results:

schedule1=1
schedule1=1
schedule1=1
schedule1=2
schedule1=2
schedule1=2
schedule1=3

[quote quote=211224]Or if you need to be able to adjust your increment frequency

PowerShell
19 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$someArray = 'value1','value2','value3','value4','value5','value6','value7'
$frequency = 3
$bodyArray = @()
$current = 0
$i = 1
$someArray | ForEach-Object {
If ($current -eq $frequency) {
$current = 0
$i++
}
$bodyArray += "schedule1=$i"
$current++
}
$bodyArray
#Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$([math]::round($someArray.count/$bodyArray.count))] | ConvertTo-Json -Depth 100) -ContentType "application/json"
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Results:
PowerShell
7 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
schedule1=1
schedule1=1
schedule1=1
schedule1=2
schedule1=2
schedule1=2
schedule1=3
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

ya something like that :smiley:

but i ended up finding an easier way! i was overthinking it

Just needed to use %, the modulus operator:

$someArray = 'value1','value2','value3','value4','value5','value6','value7'

$bodyArray = @()

$i = 0
$nth = -1

foreach($val in $someArray)
{
    $bodyArray += 'schedule1=$i','schedule2=$i','schedule3=$i'

    Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$nth)] | ConvertTo-Json -Depth 100) -ContentType "application/json"

    $i++
    if($i % ([math]::round($someArray.Count/$bodyArray.Count)) -eq 0) {$nth++}
    $bodyArray = @() #reset the array
}