PowerShell, batching and JSON endpoints
Things I've learned about PowerShell lately
Given an array of objects, if you want to batch them up to send only a few of them at a time to an endpoint accepting JSON, you can use Select-Object to skip/take a subset of the array and then use PowerShell's array notion to put the enumerable sequences back into arrays that ConvertTo-Json can accept as input:
$maxBatchSize = 10000
Do {
$batchSize = [Math]::Min($maxBatchSize, $ObjectArray.Length)
$batch = @($ObjectArray | Select-Object -First $batchSize)
$body = @{
Objects = $batch
AnotherField = $tagIds.ToArray()
YetAnotherField = @{ }
}
$body = $body | ConvertTo-Json
InvokeApiCall -Uri $url -Method PATCH -Body $body | Out-Null
$ObjectArray = $ObjectArray | Select-Object -Skip $batchSize
}
While ($ObjectArray.Length -ge $maxBatchSize)
Without the array notion - @() - above, ConvertTo-Json would complain that it could not serialize an IEnumerable.
Comments