-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepartments.ps1
214 lines (183 loc) · 6.02 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
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
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Write-Information "Processing Departments"
#region Configuration
$config = ConvertFrom-Json $configuration
#endregion Configuration
#region Support Functions
function Get-AuthToken {
[cmdletbinding()]
Param (
[string]$BaseUri,
[string]$TokenUri,
[string]$ClientKey,
[string]$ClientSecret,
[string]$PageSize
)
Process
{
$requestUri = $TokenURI
$pair = "{0}:{1}" -f $ClientKey,$ClientSecret
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$bear_token = [System.Convert]::ToBase64String($bytes)
$headers = @{
Authorization = "Basic {0}" -f $bear_token
Accept = "application/json"
}
$parameters = @{
grant_type="client_credentials"
scope='http://purl.imsglobal.org/spec/or/v1p2/scope/roster.readonly http://purl.imsglobal.org/spec/or/v1p2/scope/roster-demographics.readonly http://purl.imsglobal.org/spec/or/v1p2/scope/resource.readonly https://purl.imsglobal.org/spec/or/v1p2/scope/gradebook.readonly https://purl.imsglobal.org/spec/or/v1p2/scope/gradebook-core.readonly https://purl.imsglobal.org/spec/or/v1p2/scope/gradebook.createput'
}
Write-Information ("POST {0}" -f $requestUri)
$splat = @{
Method = 'Post'
URI = $requestUri
Body = $parameters
Headers = $headers
Verbose = $false
}
$response = Invoke-RestMethod @splat
#Write-Information $response
$accessToken = $response.access_token
#Add the authorization header to the request
$authorization = @{
Authorization = "Bearer {0}" -f $accesstoken
'Content-Type' = "application/json"
Accept = "application/json"
}
$authorization
}
}
function Get-Data {
[cmdletbinding()]
Param (
[string]$BaseUri,
[string]$TokenUri,
[string]$ClientKey,
[string]$ClientSecret,
[string]$PageSize,
[string]$EndpointUri,
[string]$PropertyName,
[string]$Filter,
[object]$Authorization
)
Begin
{
$offset = 0
$requestUri = "{0}{1}" -f $BaseURI,$EndPointUri
$propertyArray = $PropertyName
$results = [System.Collections.Generic.List[object]]::new()
}
Process
{
do
{
$parameters = [ordered]@{}
if($filter -ne $null -and $filter.Length -gt 0)
{
$parameters['filter'] = $filter
}
$parameters['limit'] =$Pagesize
$parameters['offset'] = $offset
Write-Information ("GET {0} ({1})" -f $requestUri, $offset)
$splat = @{
Method = 'GET'
Uri = $requestUri
Body = $parameters
Headers = $Authorization
Verbose = $false
}
try {
$response = Invoke-RestMethod @splat
}
catch {
if($_.Exception.Response.StatusCode.value__ -eq 401)
{
throw "Client is unauthorized"
}
elseif($_.Exception.Response.StatusCode.value__ -eq 404)
{
throw "Endpoint is not found (404) - $($requestUri)"
}
else
{
Write-Warning (" Retrying RestMethod. Error: {0}" -f $_)
Start-Sleep -seconds 5
$response = Invoke-RestMethod @splat
}
}
if($response.$propertyArray.getType().BaseType -eq [System.Array])
{
$results.AddRange($response.$propertyArray)
}
else
{
$results.Add($response.$propertyArray)
}
$offset = $offset + $response.$propertyArray.count
} while ($response.$propertyArray.count -eq $PageSize)
}
End
{
return $results
}
}
function Group-ObjectHashtable
{
param(
[string[]] $Property
)
begin
{ # create an empty hashtable
$hashtable = @{}
}
process
{ # create a key based on the submitted properties, and turn it into a string
$key = $(foreach($prop in $Property) { $_.$prop }) -join ','
# check to see if the key is present already
if ($hashtable.ContainsKey($key) -eq $false)
{ # add an empty list
$hashtable[$key] = [Collections.Generic.List[psobject]]::new()
}
# add element to appropriate array list:
$hashtable[$key].Add($_)
}
end
{ # return the entire hashtable:
$hashtable
}
}
#endregion Support Functions
#region Get Data
$splat = @{
BaseURI = $config.BaseURI
TokenUri = $config.TokenUri
ClientKey = $config.ClientKey
ClientSecret = $config.ClientSecret
PageSize = $config.PageSize
}
try {
$splat['Authorization'] = Get-AuthToken @splat
} catch {
throw "Authorization Failed - $($_)"
}
try {
$orgs = Get-Data @splat -EndpointUri "/ims/oneroster/rostering/v1p2/orgs" -PropertyName "orgs" -Filter $config.OrgFilter
} catch {
throw "Get Data Failed - $($_)"
}
#endregion Get Data
#region Return Data
$return = [System.Collections.Generic.List[psobject]]::new()
foreach($org in $orgs)
{
$department = @{
ExternalId = $org.sourcedId
DisplayName = $org.name
Name = $org.Name
Identifier = $org.identifier
Type = $org.type
}
Write-Output ($department | ConvertTo-Json -Depth 10)
}
Write-Information "Finished Processing Departments"
#endregion Return Data