-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
111 lines (94 loc) · 4 KB
/
create.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
$ServiceURL = "https://<customer>.freshservice.com"
$ApiKey = "<KEY>"
# 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
#Initialize default properties
$success = $False;
$p = $person | ConvertFrom-Json
$auditMessage = "Account for person " + $p.DisplayName + " not created successfully";
#Change mapping here
$account = @{
first_name = $p.Name.NickName;
last_name = $p.Name.FamilyName;
primary_email = $p.Contact.Business.Email
job_title = $p.Custom.PrimaryPositionDesc
}
try{
# Create authorization headers with HelloID API key
$pair = "${ApiKey}:${ApiKey}"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$key = "Basic $base64"
$headers = @{"authorization" = $Key}
# Define specific endpoint URI
if($ServiceUrl.EndsWith("/") -eq $false){ $ServiceUrl = $ServiceUrl + "/" }
$uri = "$($ServiceURL)api/v2/requesters"
try{
#Check for existing account, by email
$queryUri = ("$($uri)?email=" + ([System.Web.HTTPUtility]::UrlEncode("$($account.primary_email)")));
$response = Invoke-RestMethod -Method GET -Uri $queryUri -Headers $headers -ContentType "application/json" -Verbose:$false
if($response.requesters.id -eq $null) { throw "no result" }
$aRef = "$($response.requesters[0].id)";
$existingAccount = $response.requesters[0];
$existing = $true
}
catch
{
Write-Verbose -Verbose "No existing account."
}
if($existing -eq $true)
{
Write-Verbose -Verbose "Found existing account. $($aRef)"
#Activate account if not active
if($existingAccount.active -eq $false)
{
Write-Verbose -Verbose "Account is not active, updating active status"
if(-Not($dryRun -eq $True)) {
$activateuri = "$($ServiceURL)api/v2/requesters/$($aRef)/reactivate"
$activateResponse = Invoke-RestMethod -Method PUT -Uri $activateuri -Headers $headers -ContentType "application/json" -Verbose:$false
}
}
#Update account
Write-Verbose -Verbose "Updating existing account."
if(-Not($dryRun -eq $True)) {
$uri = "$($ServiceURL)api/v2/requesters/$($aRef)"
$body = $account | ConvertTo-Json -Depth 10
$response = Invoke-RestMethod -Method PUT -Uri $uri -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) -ContentType "application/json" -Verbose:$false
}
$success = $True;
$auditMessage = " successfully";
}
else
{
Write-Verbose -Verbose "Creating account."
if(-Not($dryRun -eq $True)) {
$body = $account | ConvertTo-Json -Depth 10
$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) -ContentType "application/json" -Verbose:$false
$aRef = "$($response.requester.id)";
$success = $True;
$auditMessage = " successfully";
}
}
}catch{
if(-Not($_.Exception.Response -eq $null)){
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$errResponse = $reader.ReadToEnd();
$auditMessage = " : ${errResponse}";
}else {
$auditMessage = " : General error $($_)";
}
}
#build up result
$result = [PSCustomObject]@{
Success = $success;
AccountReference = $aRef;
AuditDetails = $auditMessage;
Account = $account;
ExportData = [PSCustomObject]@{
primary_email = $p.Contact.Business.Email
}
};
Write-Output $result | ConvertTo-Json -Depth 10