-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-RetrieveUser.ps1
109 lines (94 loc) · 3.12 KB
/
New-RetrieveUser.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
<#
.SYNOPSIS
Connects to Retrieve Public API, creates new user and adds them to specific office group
.DESCRIPTION
Using the api documents at https://api.retrieve.com/public-api-docs/#
Authenicate using unique keyId and secret using HTTP Basic authentication.
This requires the key:secret to be UTF-8 encoded
.EXAMPLE
New-RetrieveUser -Email 'Jtest@contoso.com' -FullName 'John Test' -Office 'San Francisco'
user
----
@{id=000001; email=jtest@contoso.com}
.EXAMPLE
New-RetrieveUser 'Joetest@contoso.com' 'Joe Test' 'New York'
user
----
@{id=000001; email=Joetest@contoso.com}
.EXAMPLE
CSV Template:
Email, FullName, Office
j@test.com, J Test, New York
h@test.com, H Test, San Francisco
l@test.com, L Test, New York
PS C:\> Import-Csv -Path 'C:\Path\to\data.csv'
PS C:\> Foreach($user in $ImportedUsers){
>> New-RetrieveUser -Email $user.email -FullName $user.fullname -Office $user.office
>> }
user
----
@{id=000001; email=j@test.com}
@{id=000002; email=h@test.com}
@{id=000003; email=l@test.com}
.INPUTS
String
.NOTES
V1.0
11/1/2022
Jonathan Ahrens
#>
function New-RetrieveUser {
[CmdletBinding()]
param (
[CmdletBinding()]
[Parameter(
Mandatory,
ValueFromPipeline
)]
[String]
$Email,
[Parameter(
Mandatory,
ValueFromPipeline
)]
[String]
$FullName,
[Parameter(
Mandatory,
ValueFromPipeline
)]
[String]
$Office
)
begin {
$Vault = 'KPVaultJA'
$KeyId = (Get-Secret -Name 'Retrieve API Key' -AsPlainText -Vault $Vault)
$AppSecret = (get-Secret -Name 'Retrieve API Secret' -AsPlainText -Vault $Vault)
$KeyIdPassword = $KeyId + ':' + $appSecret
$EncodedKey = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($KeyIdPassword))
$Header =@{
'Authorization' = $EncodedKey
}
}
process {
$Body = @{
'email' = $Email
'fullName' = $FullName
}
$Url = "https://api.retrieve.com/api/v1/users/add"
Invoke-RestMethod -Method 'Post' -Uri $Url -Body $Body -Headers $Header -OutVariable Retrieveuser
Write-Verbose -Message "Created Retrive account for $FullName"
$Body = @{
'userId' = $RetrieveUser.user.id
}
switch ($office) {
'San Francisco' { $Url = "https://api.retrieve.com/api/v1/user-groups/<Insert Group ID>>/users/add"
Invoke-RestMethod -Method 'Post' -Uri $Url -Body $Body -Headers $header}
'New York' { $Url = "https://api.retrieve.com/api/v1/user-groups/<Insert Group ID>/users/add"
Invoke-RestMethod -Method 'Post' -Uri $Url -Body $Body -Headers $header}
}
Write-Verbose -Message "Added $FullName to $office Group"
}
end {
}
}