-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersons.ps1
165 lines (139 loc) · 5.7 KB
/
persons.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
Write-Information "Processing Persons"
#region Configuration
$config = ConvertFrom-Json $configuration;
$connectionString = "DRIVER={Progress OpenEdge $($config.driver_version) driver};HOST=$($config.host_name);PORT=$($config.port);DB=$($config.database);UID=$($config.user);PWD=$($config.password);DIL=$($config.isolation_mode);AS=$($config.array_size);"
if($config.enableETWT) { $connectionString += "ETWT=1;" }
if($config.enableUWCT) { $connectionString += "UWCT=1;" }
if($config.enableKA) { $connectionString += "KA=1;" }
#endregion Configuration
#region Functions
function get_data_objects {
[cmdletbinding()]
Param (
[string]$connectionString,
[string]$query
)
Process
{
$conn = (new-object System.Data.Odbc.OdbcConnection);
$conn.connectionstring = $connectionString;
$conn.open();
$cmd = (New-object System.Data.Odbc.OdbcCommand($query,$conn));
$dataSet = (New-Object System.Data.DataSet);
$dataAdapter = (New-Object System.Data.Odbc.OdbcDataAdapter($cmd));
$dataAdapter.Fill($dataSet) | Out-Null
$conn.Close()
$result = $dataset.Tables[0];
@($result);
}
}
#endregion Functions
#region Open VPN
if($config.enableVPN) {
Write-Information "Opening VPN"
#Ensure VPN Connection is closed
&"$($config.vpnClosePath)" > $null 2>&1
#Reopen VPN Connection
&"$($config.vpnOpenPath)" > $null 2>&1
}
#endregion Open VPN
#region Execute
Write-Information "Executing Staff Queries";
$staff = get_data_objects `
-connectionString $connectionString `
-query 'SELECT
"NAME"."NAME-ID"
, "NAME"."ALTERNATE-ID"
, "NAME"."FIRST-NAME"
, "NAME"."MIDDLE-NAME"
, "NAME"."LAST-NAME"
, "NAME"."NALPHAKEY"
, "NAME"."PRIMARY-PHONE"
, "NAME"."SECOND-PHONE"
, CAST("NAME"."BIRTHDATE" as date) "BIRTHDATE"
, "NAME"."INTERNET-ADDRESS"
, "NAME-DUSER"."DUSER-ID"
, "STAFF-ENTITY"."ENTITY-ID"
, "STAFF-ENTITY"."ROOM-NUMBER"
, "STAFF-TYPE"."TYPE-STAFF-ID"
, "STAFF"."STAFF-TITLE"
, "STAFF"."X-TEACHER"
, "STAFF"."X-SUBSTITUTE"
, "STAFF"."X-COUNSELOR"
FROM "PUB"."STAFF"
INNER JOIN "PUB"."NAME" ON "NAME"."NAME-ID" = "STAFF"."NAME-ID"
LEFT JOIN "PUB"."STAFF-ENTITY" "STAFF-ENTITY" ON "STAFF-ENTITY"."NAME-ID"="STAFF"."NAME-ID" AND "STAFF-ENTITY"."STATUS-CUR-YR"=''A'' AND "STAFF-ENTITY"."X-DEFAULT-ENTITY"=1
LEFT JOIN "PUB"."STAFF-TYPE" ON "STAFF-TYPE"."NAME-ID" = "STAFF"."NAME-ID"
LEFT JOIN "PUB"."NAME-DUSER" ON "NAME-DUSER"."NAME-ID" = "STAFF"."NAME-ID"
WHERE "NAME"."FIRST-NAME" NOT LIKE ''ZZ%''';
Write-Information "$($staff.count) Staff Records";
$staffEntities = get_data_objects `
-connectionString $connectionString `
-query 'SELECT * FROM "PUB"."STAFF-ENTITY"'
Write-Information "$($staffEntities.count) Staff Entity Records";
$staffDepartments = get_data_objects `
-connectionString $connectionString `
-query 'SELECT DISTINCT SD."X-DEFAULT-DEPT"
, SD."DEPARTMENT-ID"
, DEPT."DEPARTMENT-SDESC"
, DEPT."DEPARTMENT-LDESC"
, SD."ENTITY-ID"
, SD."NAME-ID"
FROM "PUB"."STAFF-DEPARTMENT" AS SD
INNER JOIN "PUB"."DEPARTMENT" AS DEPT ON SD."DEPARTMENT-ID" = DEPT."DEPARTMENT-ID"
WHERE SD."SCHOOL-YEAR" = YEAR(SYSDATE())'
Write-Information "$($staffDepartments.count) Staff Department Records";
foreach($stf in ($staff | Sort-Object 'NAME-ID' -Unique))
{
$person = @{};
$person["ExternalId"] = $stf.'NAME-ID';
$person["DisplayName"] = "$($stf.'FIRST-NAME') $($stf.'LAST-NAME') ($($stf.'NAME-ID'))"
$person["Role"] = "Employee"
foreach($prop in $stf.PSObject.properties)
{
if(@("RowError","RowState","Table","HasErrors","ItemArray") -contains $prop.Name) { continue; }
$person[$prop.Name.replace('-','_')] = "$($prop.Value)";
}
$person["Contracts"] = [System.Collections.ArrayList]@();
#Entities
foreach($assign in ($staffEntities | Sort-Object 'NAME-ID',"ENTITY-ID" -Unique))
{
if($assign.'NAME-ID' -ne $stf.'NAME-ID') { continue; }
$contract = @{};
$contract["ExternalId"] = "$($stf.'NAME-ID').$($assign.'ENTITY-ID')"
$contract["ContractType"] = "Entity"
$contract["Title"] = "$($stf.'STAFF-TITLE')"
foreach($prop in $assign.PSObject.properties)
{
if(@("RowError","RowState","Table","HasErrors","ItemArray") -contains $prop.Name) { continue; }
$contract[$prop.Name.replace('-','_')] = "$($prop.Value)";
}
[void]$person.Contracts.Add($contract);
}
#Departments
foreach($assign in ($staffDepartments | Sort-Object 'NAME-ID',"DEPARTMENT-SDESC" -Unique))
{
if($assign.'NAME-ID' -ne $stf.'NAME-ID') { continue; }
$contract = @{};
$contract["ExternalId"] = "$($stf.'NAME-ID').$($assign.'DEPARTMENT-SDESC')"
$contract["ContractType"] = "Department"
$contract["Title"] = "$($stf.'STAFF-TITLE')"
foreach($prop in $assign.PSObject.properties)
{
if(@("RowError","RowState","Table","HasErrors","ItemArray") -contains $prop.Name) { continue; }
$contract[$prop.Name.replace('-','_')] = "$($prop.Value)";
}
[void]$person.Contracts.Add($contract);
}
Write-Output ($person | ConvertTo-Json -Depth 50);
}
#endregion Execute
#region Close VPN
if($config.enableVPN) {
Write-Information "Waiting to close VPN"
Start-Sleep -s 15
Write-Information "Closing VPN"
&"$($config.vpnClosePath)" > $null 2>&1
}
#endregion Close VPN
Write-Information "Finished Processing Persons"