-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
165 lines (149 loc) · 6.68 KB
/
update.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
#Initialize default properties
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json;
$pp = $previousPerson | ConvertFrom-Json
$pd = $personDifferences | ConvertFrom-Json
$m = $manager | ConvertFrom-Json;
$aRef = $accountReference | ConvertFrom-Json;
$mRef = $managerAccountReference | ConvertFrom-Json;
$success = $False;
$auditMessage = $p.DisplayName;
#Retrieve current account data for properties to be updated
$previousAccount = [PSCustomObject]@{
email = $pp.Accounts.ActiveDirectory.mail;
}
if($pp.Accounts.ActiveDirectory.mail -eq $p.Accounts.ActiveDirectory.mail)
{
#No Changes Required
Write-Verbose -Verbose "No Changes needed."
$success = $False;
$auditMessage = $p.DisplayName;
}
else
{
#Authorization
function get_oauth_access_token {
[cmdletbinding()]
Param (
[string]$BaseURI,
[string]$ClientKey,
[string]$ClientSecret
)
Process
{
$pair = [System.Web.HTTPUtility]::UrlEncode($ClientKey) + ":" + [System.Web.HTTPUtility]::UrlEncode($ClientSecret)
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair);
$bear_token = [System.Convert]::ToBase64String($bytes);
$auth_headers = @{ Authorization = "Basic " + $bear_token };
$uri = "$($BaseURI)/oauth/token?grant_type=client_credentials";
$result = Invoke-RestMethod -Method GET -Headers $auth_headers -Uri $uri -UseBasicParsing;
@($result);
}
}
#Update Record
try {
$AccessToken = (get_oauth_access_token `
-BaseURI $c.BaseURI `
-ClientKey $c.ClientKey `
-ClientSecret $c.ClientSecret).access_token
$headers = @{ Authorization = "Bearer $($AccessToken)"; "Content-Type"= "application/json" };
#Get Existing Email Address
#Email Type
#See /Generic/1/Demographics/EmailType/1/100 for Types
$EmailTypeID = 1
#Email Rank
# 1 = Primary
$EmailRank = 1
#Search for record encase old one was deleted
$uri = "$($c.BaseURI)/Generic/1/Demographics/NameEmail/1/100?searchFields[0]=NameEmailID&searchFields[1]=EmailAddress&searchFields[2]=EmailTypeID&searchFields[3]=NameID&searchFields[4]=Rank"
$body = @{
"SearchCondition" = @{
"SearchConditionGroup" = @{
"ConditionGroupType"= "And";
"Conditions"= @(
@{
#Filter Results by NameID
"LongSearchCondition"= @{
"ConditionType"= "Equal";
"FieldName"= "NameID";
"Value"= $p.ExternalID;
}
},
@{
#Filter by Email Type
"LongSearchCondition"= @{
"ConditionType"= "Equal";
"FieldName"= "EmailTypeID";
"Value"= $EmailTypeID;
}
},
@{
#Filter by Email Rank
"LongSearchCondition"= @{
"ConditionType"= "Equal";
"FieldName"= "Rank";
"Value"= $EmailRank;
}
}
)
}
}
}
$currentRecords = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body ($body | ConvertTo-Json -Depth 50)
#Check if Records Exist
if($currentRecords.Objects.Count -gt 0)
{
Write-Verbose -Verbose "Found Existing Email Record";
#Use existing record, update
$account = [PSCustomObject]@{
DataObject = $currentRecords.Objects[0]
}
}
else
{
#No existing records, create new record
Write-Verbose -Verbose "No Existing Email Record Found"
$account = [PSCustomObject]@{
DataObject = @{
EmailAddress = "";
EmailTypeID = $EmailTypeID;
NameID = $p.ExternalID;
Rank = $EmailRank;
}
}
}
#Change mapping here
$account.DataObject.EmailAddress = $p.Accounts.ActiveDirectory.mail;
if(-Not($dryRun -eq $True)) {
if($account.DataObject.NameEmailID -eq $null)
{
#Create
Write-Verbose -Verbose "Creating new email record";
$uri = "$($c.BaseURI)/Generic/1/Demographics/NameEmail?searchFields[0]=NameEmailID&searchFields[1]=EmailAddress&searchFields[2]=EmailTypeID&searchFields[3]=NameID&searchFields[4]=Rank"
$result = Invoke-RestMethod -Method PUT -Uri $uri -Headers $headers -Body ($account | ConvertTo-Json -Depth 20) -ContentType "application/json" -UseBasicParsing;
$aRef = $result.NameEmailID;
}
else
{
#Update
Write-Verbose -Verbose "Updating existing email record";
$uri = "$($c.BaseURI)/Generic/1/Demographics/NameEmail/$($account.DataObject.NameEmailID)?searchFields[0]=NameEmailID&searchFields[1]=EmailAddress&searchFields[2]=EmailTypeID&searchFields[3]=NameID&searchFields[4]=Rank"
$result = Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body ($account | ConvertTo-Json -Depth 20) -ContentType "application/json" -UseBasicParsing;
$aRef = $result.NameEmailID;
}
}
$success = $True;
$auditMessage = " successfully";
}catch{
$auditMessage = " : General error $($_)";
Write-Error -Verbose $_;
}
}
#build up result
$result = [PSCustomObject]@{
Success= $success;
AuditDetails=$auditMessage;
Account = $account;
PreviousAccount = $previousAccount;
};
Write-Output $result | ConvertTo-Json -Depth 10;