-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersons.ps1
More file actions
140 lines (115 loc) · 3.94 KB
/
persons.ps1
File metadata and controls
140 lines (115 loc) · 3.94 KB
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
#Configuration
$config = ConvertFrom-Json $configuration;
$expansions = $config.expansions -split (",");
$extensions = $config.extensions -split (",");
#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"
}
#Get Schools
$uri = "$($config.baseurl)/ws/v1/district/school"
$schools = (Invoke-RestMethod $uri -Method GET -Headers $headers).schools.school
$allstaff = [System.Collections.ArrayList]@();
foreach($school in $schools)
{
#Count Staff
$uri = "$($config.baseurl)/ws/v1/school/$($school.id)/staff/count"
$count = (Invoke-RestMethod $uri -Method GET -Headers $headers -Body $parameters).resource.count
#Get Staff
Write-Verbose -Verbose "Retrieving Staff ($($school.id))"
$page = 1;
$staff = [System.Collections.ArrayList]@();
while($true)
{
$parameters = @{
expansions = ($expansions -join ',');
page = $page;
pagesize = 100
}
$uri = "$($config.baseurl)/ws/v1/school/$($school.id)/staff"
Write-Verbose -Verbose "Page $($page)";
$response = Invoke-RestMethod $uri -Method GET -Headers $headers -Body $parameters
if($response.staffs.staff -eq $null) { break; }
if($response.staffs.staff -is [array])
{
[void]$staff.AddRange($response.staffs.staff);
}
else
{
[void]$staff.Add($response.staffs.staff);
}
if($staff.count -lt $count)
{
$page++;
}
else
{
break;
}
}
if($staff.count -gt 0)
{
[void]$allStaff.AddRange($staff);
}
}
function Get-ObjectProperties
{
param ($Object, $Depth = 0, $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);
}
elseif ($prop.TypeNameOfValue -eq "System.Object[]")
{
$OutObject[$prop.Name] = [System.Collections.ArrayList]@()
foreach($item in $prop.Value)
{
$OutObject[$prop.Name].Add($item)
}
}
else
{
$OutObject[$prop.Name] = "$($prop.Value)"
}
}
return $OutObject;
}
function Get-ExtensionData
{
param ($Object)
$OutObject = @{};
foreach($item in $Object.'_table_extension')
{
foreach($field in $item.'_field')
{
$OutObject[$field.Name] = $field.value
}
}
return $OutObject;
}
foreach($s in $allstaff)
{
$person = @{};
$person = Get-ObjectProperties -Object $s;
$person['extensionData'] = Get-ExtensionData $s.'_extension_data'
$person['ExternalId'] = if($s.id) { $s.Id } else { $s.local_id }
$person['DisplayName'] = "$($s.Name.first_name) $($s.name.last_name) ($($person.ExternalId))";
$person['Contracts'] = [System.Collections.ArrayList]@();
foreach($school in $s.school_affiliations.school_affiliation)
{
$contract = @{};
$location = @{};
$contract = Get-ObjectProperties -Object $school;
$contract['PersonExternalId'] = $($person.ExternalId)
[void]$person['Contracts'].Add($contract);
}
Write-Output ($person | ConvertTo-Json -Depth 20);
}