-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas_disenrol_student.ps1
87 lines (64 loc) · 3.02 KB
/
canvas_disenrol_student.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# This disenrolls a student from a sub-set of their Canvas courses. It is currently
# set to disenroll Y11 students from all IB courses, which is useful when students
# leave the IB program.
# Make all errors terminating
$ErrorActionPreference = "Stop"
################################################################################
# GLOBAL VARIABLES
################################################################################
$token = "<TOKEN>"
$headers = @{"Authorization"="Bearer "+$token}
$userId = 2478
$allEnrollments = @()
$enrollmentsToRemove = @()
################################################################################
# GET ALL ENROLLMENTS FOR STUDENT
################################################################################
# Get an array of enrollment objects for this user.
$response = Invoke-RestMethod `
-URI "https://<HOSTNAME>:443/api/v1/users/$userId/enrollments?type=StudentEnrollment" `
-headers $headers `
-method GET `
-FollowRelLink
foreach($page in $response){
$allEnrollments += $page
}
Write-Host "$($allEnrollments.count) enrollments found for user with ID $userId."
################################################################################
# GET LIST OF ENROLLMENTS TO REMOVE BASED ON MATCH CONDITION
################################################################################
foreach($enrollment in $allEnrollments) {
# Get course name for this enrollment.
$course = Invoke-RestMethod `
-URI "https://<HOSTNAME>:443/api/v1/courses/$($enrollment.course_id)" `
-headers $headers `
-method GET
# Write-Host "$($course.id) - $($course.Name)"
# This currently filters for only Y11 IB courses. Useful when students drop out of the IB program.
if ($course.name -match '^.*11.*IB.*$') {
$enrollmentsToRemove += @{Id = $enrollment.id; CourseName = $course.name; CourseId = $course.id}
}
}
################################################################################
# CONFIRM WITH USER
################################################################################
Write-Host "The following enrollments will be removed:"
$enrollmentsToRemove | Select-Object CourseId, CourseName | Format-List
do {
$k = Read-Host "OK to proceed? (Y/N)"
} until ($k -match 'Y|N' -and $k.length -EQ 1)
if ($k -EQ 'N') {
Write-Host "Aborted action. Exiting"
Return
}
################################################################################
# REMOVE ENROLLMENTS
################################################################################
foreach($enrollment in $enrollmentsToRemove) {
$course = Invoke-RestMethod `
-URI "https://<HOSTNAME>:443/api/v1/courses/$($enrollment.courseId)/enrollments/$($enrollment.id)" `
-headers $headers `
-method DELETE
Write-Host "Removed enrollment #$($enrollment.id) for course #$($enrollment.courseId) - $($enrollment.courseName)."
}
Write-Host "Finished."