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
Olaf
March 18, 2020, 8:37pm
2
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])
}
Olaf
March 18, 2020, 8:59pm
4
Sorry, I cannot follow. Maybe it’s too late for me.
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
Olaf
March 18, 2020, 9:34pm
6
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
<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:
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
schedule1 = 1
schedule1 = 1
schedule1 = 1
schedule1 = 2
schedule1 = 2
schedule1 = 2
schedule1 = 3
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
ya something like that
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
}