-
Notifications
You must be signed in to change notification settings - Fork 0
/
persons.ps1
97 lines (81 loc) · 3.24 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
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
#Initial Setup
$config = ConvertFrom-Json $configuration;
#Instance URL
$baseURI = "https://api.bamboohr.com/api/gateway.php/$($config.companySubDomain)";
#Authorization
$headers = @{};
$headers["Authorization"] = "Basic $([System.Convert]::ToBase64String( [System.Text.Encoding]::ASCII.GetBytes("$($config.apiKey):x") );)";
$headers["Accept"] = "application/json";
#Functions
function Get-ObjectProperties
{
param ($Object, $Depth = 0, $MaxDepth = 10)
$OutObject = @{};
foreach($prop in $Object.PSObject.properties)
{
if($prop.Name -in @("ExternalId","DisplayName","Contracts")) { continue; }
if ($prop.TypeNameOfValue -eq "System.Management.Automation.PSCustomObject" -or $prop.TypeNameOfValue -eq "System.Object" -and $Depth -lt $MaxDepth -and $prop.Value -ne $null)
{
$OutObject[$prop.Name] = Get-ObjectProperties -Object $prop.Value -Depth ($Depth + 1);
}
else
{
$OutObject[$prop.Name] = "$($prop.Value)";
}
}
return $OutObject;
}
#Get All Employees
$parameters = @{
Headers = $headers;
Method = 'GET'
Uri = "$($baseURI)/v1/employees/directory"
}
Write-Information "Fetching Employees [$($parameters.Uri)]";
$persons = Invoke-RestMethod @parameters
Write-Information "Completed Employees Request";
#Get All Employee Jobs
$parameters = @{
Headers = $headers;
Method = 'GET'
Uri = "$($baseURI)/v1/employees/all/tables/jobInfo"
}
Write-Information "Fetching Employee Jobs [$($parameters.Uri)]";
$jobs = Invoke-RestMethod @parameters
Write-Information "Completed Employee Jobs Request";
Write-Information "Processing Employees";
foreach($p in $persons.employees)
{
$parameters = @{
Headers = $headers;
Method = 'GET'
Uri = "$($baseURI)/v1/employees/$($p.id)"
Body = @{
fields = $config.employeeFields
}
}
Write-Information "Fetching Employee Data [$($parameters.Uri)]";
$employee = Invoke-RestMethod @parameters
$person = @{};
$person = Get-ObjectProperties $employee
$person['ExternalId'] = $p.id;
$person['DisplayName'] = "$($p.firstName) $($p.lastName) - $($person['ExternalId'])";
$person['Contracts'] = [System.Collections.ArrayList]@();
foreach($job in $jobs)
{
if($job.employeeId -eq $p.id)
{
$contract = {};
$contract = Get-ObjectProperties $job
$contract['ExternalId'] = $job.id;
$contract['terminationDate'] = $employee.terminationDate;
$contract['hireDate'] = $employee.hireDate;
$contract['originalHireDate'] = $employee.originalHireDate;
[void]$person.Contracts.Add($contract);
}
}
Write-Output ($person | ConvertTo-Json -Depth 10);
}
Write-Information "Finished Processing Employees";
Write-Verbose -Verbose "Person import completed";