'Using PowerShell to send DELETE API request
I am using PowerShell to read a database, get a list of user IDs and then delete everyone from a particular course. Because the course contains over 1000 records I have added pagination (per_page=100) to my request statement. However, my delete statement only removes 100 records at a time. When I re-run it, it will remove the next 100 and so on. I want it to remove everyone at once. I think that I need to place a Do While loop on my API requests but am not sure if that is the proper way to do it. Any assistance would be greatly appreciated. I have included the code here.
$RequestURL = "$domainurl/$CourseID/enrollments?type[]=StudentEnrollment&state[]=active&per_page=100"
$Results = Invoke-WebRequest -Uri $RequestURL -Headers $headers -Method GET -UseBasicParsing
$json = $Results | ConvertFrom-Json # Convert the results from JSON
foreach($obj in $json) {
$ID = $obj.id # user id
$URL = '$domainurl' + $CourseID + '/enrollments/' + $ID + '?task=delete'
Invoke-WebRequest -Uri $Url -Headers $headers -Method Delete -UseBasicParsing | write-host
}
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
