-
Notifications
You must be signed in to change notification settings - Fork 0
/
sds-asm.ps1
163 lines (137 loc) · 5.09 KB
/
sds-asm.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#requires -psedition core
$uri = $env:OR_URL# "https://or.localhost/ims/oneroster/v1p1"
$ci = $env:OR_CI # API clientid
$cs = $env:OR_CS # API clientsecret
# check for CEDS conversion commandlet
try { ConvertFrom-K12 }
catch {
write-error "Missing cmdlet: ConvertFrom-K12"
break
}
# Test/create csv directory
if (!(test-path ./csv-asm)) {
new-item -itemtype directory -path ./csv-asm
}
if (!$env:GOORS_TOKEN) {
$loginP = @{
uri = "$uri/login"
method = "POST"
body = "clientid=$ci&clientsecret=$cs"
SkipCertificateCheck = $true
}
$env:GOORS_TOKEN = Invoke-RestMethod @loginP
}
$getP = @{
method = "GET"
headers = @{"Authorization" = "bearer $ENV:GOORS_TOKEN" }
FollowRelLink = $true
SkipCertificateCheck = $true
}
# locations csv
$orgsGet = invoke-restmethod @getP -uri "$uri/orgs"
$orgs = $orgsGet.orgs |
select-object @{n = 'location_id'; e = { $_.sourcedid } },
@{n = 'location_name'; e = { $_.name } }
$orgs | export-csv ./csv-asm/locations.csv
# users
$usersGet = invoke-restmethod @getP -uri "$uri/users"
# blacklist
$blacklistUsers = $usersGet.Users |
Select-Object *, @{ n = 'YearIndex'; e = { convertfrom-k12 -Year $_.grades -ToIndex } } |
Where-Object {
($_.status -eq 'tobedeleted') -or
($_.email -eq 'NULL') -or
($_.familyName -like '*ACCOUNT*') -or
($_.YearIndex -ge 0 -and $_.YearIndex -le 3) -or
($_.YearIndex -ge 10 -and $_.YearIndex -le 16)
}
# staff csv
$usersTeachers = $usersGet.users |
Where-Object role -eq 'teacher' |
Where-Object status -eq 'active' |
Where-Object SourcedId -notin $blacklistUsers.sourcedId |
Select-Object @{n = 'person_id'; e = { $_.SourcedId } },
@{n = 'person_number'; e = { $null } },
@{n = 'first_name'; e = { $_.givenName } },
@{n = 'middle_name'; e = { $null } },
@{n = 'last_name'; e = { $_.familyName } },
@{n = 'email_address'; e = { $_.email } },
@{n = 'sis_username'; e = { $null } },
@{n = 'location_id'; e = { $_.orgs.SourcedId -join ',' } }
$usersTeachers | export-csv ./csv-asm/staff.csv
# students csv
$userPupil = $usersGet.users |
Where-Object role -eq 'student' |
Where-Object status -eq 'active' |
Where-Object SourcedId -notin $blacklistUsers.sourcedId |
Select-Object @{n = 'person_id'; e = { $_.SourcedId } },
@{n = 'person_number'; e = { $null } },
@{n = 'first_name'; e = { $_.givenName } },
@{n = 'middle_name'; e = { $null } },
@{n = 'last_name'; e = { $_.familyName } },
@{n = 'grade_level'; e = { $null } },
@{n = 'email_address'; e = { $_.email } },
@{n = 'sis_username'; e = { $null } },
@{n = 'password_policy'; e = { "4" } },
@{n = 'location_id'; e = { $_.orgs.SourcedId -join ',' } }
$userPupil | export-csv ./csv-asm/students.csv
# courses csv
$coursesGet = invoke-restmethod @getP -uri "$uri/courses?filter=courseCode='TG'"
$courses = $coursesGet.courses |
select-object @{n = 'course_id'; e = { $_.sourcedId } },
@{n = 'course_number'; e = { $_.courseCode } },
@{n = 'course_name'; e = { $_.title } },
@{n = 'location_id'; e = { $_.org.sourcedId } }
$courses | export-csv ./csv-asm/courses.csv
$enrollmentsGet = invoke-restmethod @getP -uri "$uri/enrollments?filter=status='active'"
# instructors for classes
$enrollmentsInst = $enrollmentsGet.enrollments |
Where-Object role -eq 'teacher' |
Where-Object { $_.user.sourcedid -notin $blacklistUsers.sourcedId }
# classes csv
$classesGet = invoke-restmethod @getP -uri "$uri/classes?filter=status='active'"
# blacklist
$blacklistClasses = $classesGet.classes |
Where-Object {
{ $_.classType -ne 'homeroom' }
} |
Select-Object *, @{ n = 'YearIndex'; e = { convertfrom-k12 -Year $_.grades -ToIndex } } |
Where-Object { $_.YearIndex -le 3 }
#classes
$classes = $classesGet.classes |
Where-Object classType -eq 'homeroom' |
Where-object sourcedid -notin $blacklistClasses.sourcedId |
select-object @{n = 'class_id'; e = { $_.sourcedId } },
@{n = 'class_number'; e = { $_.classCode } },
@{n = 'course_id'; e = { $_.course.sourcedId } },
@{n = 'instructor_id'; e = { $null } },
@{n = 'instructor_id_2'; e = { $null } },
@{n = 'instructor_id_3'; e = { $null } },
@{n = 'location_id'; e = { $_.school.sourcedId -join ',' } }
# merge instructors
foreach ($c in $classes) {
$i = $enrollmentsInst | where-object { $_.class.sourcedId -eq $c.class_id }
if ($i) {
$c.instructor_id = $i[0].user.sourcedId
}
if ($i.count -gt 1) {
$n = 2
foreach ($t in $i[1..2]) {
$c."instructor_id_$n" = $t.user.sourcedId
$n++
}
}
}
$classes | export-csv ./csv-asm/classes.csv
# roster csv
$enrollmentsStu = $enrollmentsGet.enrollments |
Where-Object role -eq 'student' |
Where-Object { $_.class.sourcedid -notin $blacklistClasses.sourcedId } |
Where-Object { $_.user.sourcedid -notin $blacklistUsers.sourcedId } |
select-object @{n = 'roster_id'; e = { $_.sourcedId } },
@{n = 'class_id'; e = { $_.class.sourcedId } },
@{n = 'student_id'; e = { $_.user.sourcedId } }
$enrollmentsStu | export-csv ./csv-asm/rosters.csv
# export
$d = Get-Date -Format yyyy-MM-ddTHH-mm-ss
Compress-Archive ./csv-asm/*.csv "./csv-asm-$d.zip"