-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLR-Admin-API_BulkIdUpdate-CSV.ps1
More file actions
165 lines (144 loc) · 4.17 KB
/
LR-Admin-API_BulkIdUpdate-CSV.ps1
File metadata and controls
165 lines (144 loc) · 4.17 KB
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
param(
[string]$CSVFileName,
[string]$PMHost,
[string]$token,
[int]$EntityID = 1
)
Function TrustAllCerts
{
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
}
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Title = "Select Users file to import, file format it should be First,Middle,Last,Username"
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "Comm-Separtaed (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$FileName = $OpenFileDialog.filename
if($FileName -eq "")
{
exit 1
}
else
{
return $FileName
}
}
Function Get-CSV($CSVFileName)
{
try
{
$CSV = Import-Csv -Path $CSVFileName
return $CSV
}
catch
{
Write-Error "Exception $_.Exception.Message"
exit 1
}
}
Function GetUserId
{
param(
[string]$identifier
)
}
Function BuildJSON
{
param(
[string]$ID,
[string]$first,
[string]$middle,
[string]$last,
[string]$username,
[string]$title
)
if($ID -eq "")
{
$ID = [guid]::NewGuid()
}
$source = @()
$source += [pscustomobject]@{
"AccountName" = "$first $middle $last"
"IAMName" = "CSV"
}
$identifiers = @()
$identifiers += [pscustomobject]@{
"identifierType" = "Login"
"value" = "$username"
"recordStatus" = "New"
"source" = $source
}
$accounts = @()
$accounts += [pscustomobject]@{
"thumbnailPhoto" = ""
"vendorUniqueKey" = $ID
"hasOwnerIdentity" = $true
"hasSameRootEntityAsTarget" = $true
"isPrimary" = $true
"accountType" = "Custom"
"login" = "$username"
"nameFirst" = "$first"
"nameMiddle" = "$middle"
"nameLast"= "$last"
#"company" = ""
#"department" = "string"
"title" = "$title"
#"manager" = "string"
#"addressCity" = "string"
#"domainName" = "string"
"identifiers" = $identifiers
}
$JSONDoc = [pscustomobject]@{
"friendlyName" = "API"
"accounts" = $accounts
}
$JSON = ($JSONDoc | ConvertTo-Json -Depth 5)
#Write-Host $JSON
return $JSON
}
##Main
$token = Read-Host -Prompt "Please input API Token genrated from Client Console"
$PMHost = Read-Host -Prompt "Please input PM Host IP/Name (localhost) to keep default hit Enter"
if($PMHost -eq "")
{
$apiUrl = "http://localhost:8505"
}
else
{
$apiUrl = "https://" + $PMHost + ":8501"
TrustAllCerts
}
Write-Host $apiUrl
write-host "Please select CSV file CSV, file format it should be First,Middle,Last,Username"
$CSVFileName = Get-FileName([System.IO.Directory]::GetCurrentDirectory())
$CSVDoc = Get-CSV($CSVFileName)
#Write-Host $CSVDoc.Count
#Invoke-WebRequest -Uri http://$apiURL/lr-admin-api/identities/$id/ -Headers @{"Authorization" = "Bearer $token"} -ContentType "application/json" -Method GET -UseBasicParsing
foreach ($Entry in $CSVDoc)
{
[string]$F = $($Entry.Firstname)
[string]$M = $($Entry.Middlename)
[string]$L = $($Entry.Lastname)
[string]$U = $($Entry.Username)
[string]$T = $($Entry.Title)
$IdJSON = BuildJSON $F $M $L $U $T
Write-Host $IdJSON
#Add Identity to default Entity
$result = Invoke-WebRequest -Uri $apiURL/lr-admin-api/identities/bulk?entityID=$EntityID -Headers @{"Authorization" = "Bearer $token"} -ContentType 'application/json' -Method Post -Body $IdJSON
Write-host $result
}