1

Have a simple array of serial numbers, $arySerials. I use them to make a web services call. The web service API only accepts 50 serial numbers at a time. So, I want to send 50 from $arySerials, then remove those 50 from the original array with the working array $arySerialsWorking. Without iterating through a ForEach.

while ($arySerialNumbers.count -ne 0)
{
     $arySerialsWorking = $arySerialNumbers[0..49]
     $strSerialsWorkingOut = $arySerialsWorking -join ","
     $objOutContractInformationArray += Get-ASContractInformation -arySerialNumbers $strSerialsWorkingOut
     $arySerialNumbers = $arySerialNumbers | where {$_ -ne $arySerialsWorking }

}

I want:

$arySerialNumbers = $arySerialNumbers | where {$_ -ne $arySerialsWorking }

To remove the already used serial numbers. Until eventually $arySerialNumbers.count will end up at 0. I want to do this without doing a long ForEach Itteration.

1
  • Since an array object is of fixed size, you can't remove items. You can filter the array and set the result to a new array. You can also cast the array to a different type that supports removal like [system.collections.arraylist]. You could alternatively use a for loop and increment your index by 50 on each end of the range for each iteration. Commented Sep 10, 2019 at 18:05

1 Answer 1

1

You could do the following to produce the desired effect.

$objOutContractInformationArray = for ($i = 0; $i -lt $arySerialNumbers.count; $i += 50) {
    $arySerialsWorking = $arySerialNumbers[$i..($i+49)]
    $strSerialsWorkingOut = $arySerialsWorking -join ","
    Get-ASContractInformation -arySerialNumbers $strSerialsWorkingOut
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is perfect. I really appreciate it. I wish my upvote counted!
You mind adding a "+=" instead of an "=" on line 4 to make it correct.
Sure. Do note that += to rebuild arrays is not ideal or efficient. Doing that requires having to expand the array every time that operation occurs. You are better off outputting your command in the loop and setting the entire loop output to your array. An example would be $objOutContractInfoArray = for () { Get-AsContractInformation }
I have updated the post to do the array build more efficiently. Let me know if that works.
I had no idea you could do that. Thanks for taking the time to show me. I've implemented your solution, works like a champ. (Just need to change my typo of "$arySerialsWorkingOut " to "$strSerialsWorkingOut ".

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.