使用PowerShell发送DELETE API请求

发布于 2025-01-10 13:05:36 字数 795 浏览 0 评论 0原文

我正在使用 PowerShell 读取数据库,获取用户 ID 列表,然后删除特定课程中的所有人。由于课程包含超过 1000 条记录,我已在请求语句中添加了分页 (per_page=100)。但是,我的删除语句一次仅删除 100 条记录。当我重新运行它时,它将删除接下来的 100 个,依此类推。我希望它立即删除所有人。我认为我需要在 API 请求上放置一个 Do While 循环,但不确定这是否是正确的方法。任何帮助将不胜感激。我已在此处包含代码。

    $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
    }

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文