-
Notifications
You must be signed in to change notification settings - Fork 2
/
PowerShell-PowerSchool.psm1
294 lines (252 loc) · 7.88 KB
/
PowerShell-PowerSchool.psm1
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<#
.SYNOPSIS
This module contains functions for working with the PowerSchool API in PowerShell
.DESCRIPTION
.LINK
https://support.powerschool.com/developer/#/
.NOTES
Authors: Charles Crossan
.VERSION
1.0.3
#>
function Connect-PowerSchoolService {
<#
.PARAMETER Username
ClientID for OAUTH pplugin
.PARAMETER Password
$password = ConvertTo-SecureString –String "password" –AsPlainText -Force
#>
param (
[parameter(Mandatory=$true)]
[String]
$ClientID,
[String]
$ClientSecret,
[String]
$PowerSchoolURL
)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", 'application/x-www-form-urlencoded;charset=UTF-8')
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $ClientID,$ClientSecret)))
$headers.add("Authorization","Basic $base64AuthInfo")
$body = "grant_type=client_credentials"
$Response = Invoke-RestMethod -Uri $PowerSchoolURL"/oauth/access_token" -Method Post -Headers $headers -Body $body
Set-Variable -Scope Global -Name "PowerSchoolURL" -Value $PowerSchoolURL
Set-Variable -Scope Global -Name "PowerSchoolAccessToken" -Value $Response.access_token
Set-Variable -Scope Global -Name "PowerSchoolAccessTokenExpiration" -Value $(Get-Date).AddSeconds($Response.expires_in)
}
function Invoke-PowerSchoolRESTMethod {
param(
$EndpointURL,
$Method,
$PageNumber=0,
$PageSize=0,
$Body = $null
)
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.add("Authorization","Bearer $($(Get-Variable -Name "PowerSchoolAccessToken").Value)")
$headers.Add("Accept", 'application/json')
$headers.Add("Content-Type",'application/json')
if($PageNumber -gt 0)
{
if ($EndpointURL.Contains("?"))
{
$EndpointURL +="&page=$PageNumber"
}
else {
$EndpointURL +="?page=$PageNumber"
}
}
if ($PageSize -lt 100 -and $PageSize -gt 0)
{
if ($EndpointURL.Contains("?"))
{
$EndpointURL +="&pagesize=$PageSize"
}
}
$uri ="$($(Get-Variable -Name 'PowerSchoolURL').value)$($EndpointURL)"
if ($Body)
{
$Response = Invoke-RestMethod -Uri $uri -Method $Method -Headers $headers -Body $Body
}
else
{
$Response = Invoke-RestMethod -Uri $uri -Method $Method -Headers $headers
}
$Response
}
function Get-PowerSchoolRecordCount {
param(
$EndpointURL
)
$split = $EndpointURL.split('?')
$URL = "$($split[0])/count?$($split[1])"
Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" | Select-Object -ExpandProperty Resource | Select-Object -ExpandProperty Count
}
function Get-PowerSchoolAttendanceRecords {
param(
$Date = $(get-date -format "MM-dd-yyyy"),
$AttendanceCodes = "1"
)
$attendanceRecords = @()
$postBody ="{ ""date"": ""$Date"" }"
$pageCounter = 0
$hasMore = $true
While ( $hasMore )
{
$qr = Execute-PowerSchoolPowerQuery -queryName "org.bucksiu.powershellpowerschool.api.dailyattendance" -PageNumber $pageCounter -postBody $postBody
if ($qr.record.count -lt 100)
{
$hasMore = $false
}
Foreach ($attendanceRecord in $qr.record)
{
$attendanceRecords += $attendanceRecord.tables.attendance
}
$pageCounter +=1
}
$attendanceRecords
}
function Get-PowerSchoolStudents {
<#
.LINK
https://support.powerschool.com/developer/#/page/student-resources
.LINK
https://support.powerschool.com/developer/#/page/data-dictionary#student
.LINK
https://support.powerschool.com/developer/#studentextensionresource
#>
param(
[int16]
$MaxResults=0,
[int16]
$SchoolID = 3,
[String[]]
$EnrollmentStatus = @("A","P"),
[String[]]
$Expansions = @("demographics","addresses","alerts","phones","school_enrollment","ethnicity_race","contact","contact_info","initial_enrollment","schedule_setup","fees", "lunch"),
[String[]]
$Extensions=@("s_pa_stu_x","s_stu_crdc_x","c_studentlocator","s_stu_ncea_x","studentcorefields"),
[Switch]
$UseQuery,
[string]
$QueryName = "org.bucksiu.powershellpowerschool.api.students"
)
$studentResults = @()
if( $UseQuery )
{
$pageCounter = 0
$hasMore = $true
While ( $hasMore )
{
$qr = Execute-PowerSchoolPowerQuery -queryName $QueryName -PageNumber $pageCounter
if ($qr.record.count -lt 100)
{
$hasMore = $false
}
Foreach ($Student in $qr.record)
{
$studentResults += $student.tables.students
}
$pageCounter +=1
}
}
else {
$URL = "/ws/v1/district/student?q=school_enrollment.enroll_status==($($EnrollmentStatus -join ','))"
if ($Expansions.count -gt 0)
{
$URL ="$URL&expansions=$($Expansions -join ',')"
}
if ($Extensions.count -gt 0)
{
$URL ="$URL&extensions=$($Extensions -join ',')"
}
$count = Get-PowerSchoolRecordCount -EndpointURL $URL
$pageCounter = 0
While ( $studentResults.Count -lt $count)
{
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" -PageNumber $pageCounter -PageSize $MaxResults
Foreach ($student in $response.students.student)
{
$studentResults += $student
}
$pageCounter +=1
}
}
$studentResults
}
function Get-PowerSchoolDatabaseTables {
$URL = "/ws/schema/table"
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" -PageNumber $pageCounter -PageSize $MaxResults
$response
}
Function Get-PowerSchoolTableSchema {
param(
$tableName
)
$URL = "/ws/schema/table/$tableName/metadata"
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" -PageNumber $pageCounter -PageSize $MaxResults
$response
}
function Get-PowerSchoolTableRecord {
param(
$tableName,
$id,
[string[]]
$columns=@("*")
)
$URL = "/ws/schema/table/$tableName/$($id)?projection=$($columns -join ',')"
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" -PageNumber $pageCounter -PageSize $MaxResults
$response
}
function Get-PowerSchoolCourses {
param(
$SchoolID= 3,
[String[]]
$Extensions=@("s_pa_crs_x","s_crs_crdc_x")
)
$URL = "/ws/v1/school/$SchoolId/course"
if ($Extensions.count -gt 0)
{
$URL ="$($URL)?extensions=$($Extensions -join ',')"
}
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "GET" -PageNumber $pageCounter -PageSize $MaxResults
$response
}
function Execute-PowerSchoolPowerQuery {
<#
.LINK
https://support.powerschool.com/developer/#/page/powerqueries
#>
param (
$queryName,
$PageNumber=0,
$postBody=$null
)
$URL = "/ws/schema/query/$queryName"
$response = Invoke-PowerSchoolRESTMethod -EndpointURL $URL -Method "POST" -PageNumber $PageNumber -Body $postBody
$response
}
function Get-PowerSchoolUsers {
$users = @()
$pageCounter = 0
$hasMore = $true
While ( $hasMore )
{
$qr = Execute-PowerSchoolPowerQuery -queryName "org.bucksiu.powershellpowerschool.api.users" -PageNumber $pageCounter
if ($qr.record.count -lt 100)
{
$hasMore = $false
}
Foreach ($userRecord in $qr.record)
{
$users += $userRecord.tables.users
}
$pageCounter +=1
}
$users
}
function New-PowerSchoolUser {
param(
)
}