-
Notifications
You must be signed in to change notification settings - Fork 0
/
departments.ps1
143 lines (124 loc) · 3.93 KB
/
departments.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
#region Configuration
$config = ConvertFrom-Json $configuration;
$expansions = $config.expansions -split (",");
$extensions = $config.extensions -split (",");
# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
#endregion Configuration
#region Functions
function New-AccessToken() {
[cmdletbinding()]
Param (
[object]$config
)
Process
{
#Get OAuth Token
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
$Token = [System.Convert]::ToBase64String( [System.Text.Encoding]::ASCII.GetBytes("$($config.apiKey):$($config.apiSecret)") );
$headers = @{ Authorization = "Basic " + $Token };
$tokenResponse = Invoke-RestMethod -uri "$($config.baseurl)/oauth/access_token" -Method 'POST' -Headers $headers -Body (@{grant_type= "client_credentials";})
$headers = @{
"Authorization"= "Bearer $($tokenResponse.access_token)"
"Accept"= "application/json"
}
return $headers;
}
}
function Get-ObjectProperties
{
[cmdletbinding()]
param (
[object]$Object,
[int]$Depth = 0,
[int]$MaxDepth = 10
)
$OutObject = @{};
foreach($prop in $Object.PSObject.properties)
{
if ($prop.TypeNameOfValue -eq "System.Management.Automation.PSCustomObject" -or $prop.TypeNameOfValue -eq "System.Object" -and $Depth -lt $MaxDepth)
{
$OutObject[$prop.Name] = Get-ObjectProperties -Object $prop.Value -Depth ($Depth + 1);
}
else
{
$OutObject[$prop.Name] = "$($prop.Value)";
}
}
return $OutObject;
}
function Get-ErrorMessage
{
[cmdletbinding()]
param (
[object]$Response
)
try {
$reader = New-Object System.IO.StreamReader($Response.Exception.Response.GetResponseStream())
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
Write-Error "StatusCode: $($Response.Exception.Response.StatusCode.value__)`nStatusDescription: $($Response.Exception.Response.StatusDescription)`nMessage: $($reader.ReadToEnd())"
} catch
{
Write-Information $response
}
}
#endregion Functions
#region Execute
try {
$headers = New-AccessToken -Config $config;
#Get Schools
Write-Information "Retrieving Departments (Schools)"
$uri = "$($config.baseurl)/ws/v1/district/school/count"
$count = (Invoke-RestMethod $uri -Method GET -Headers $headers ).resource.count
Write-Information "$($count) Schools returned"
if($count -eq 0 -or $count -eq $null){throw "Invalid result count for departments"}
$page = 1;
$schools = [System.Collections.ArrayList]@();
while($true)
{
$parameters = @{
page = $page;
pagesize = $config.pageSize;
}
$uri = "$($config.baseurl)/ws/v1/district/school"
try {
Write-Information "Retrieving $($uri) - Page $($page)"
$response = Invoke-RestMethod $uri -Method GET -Headers $headers -Body $parameters
}
catch {
Get-ErrorMessage -response $_;
throw $_;
}
if($response.schools.school -is [array])
{
[void]$schools.AddRange($response.schools.school);
}
else
{
[void]$schools.Add($response.schools.school);
}
if($schools.count -lt $count)
{
$page++;
}
else
{
break;
}
}
foreach($s in $schools)
{
$row = @{
ExternalId = $s.id;
DisplayName = $s.name;
Code = $s.school_number;
}
$row | ConvertTo-Json -Depth 10
}
}
catch
{
throw "Failed to get schools - $_"
}
#endregion Execute