-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.ps1
More file actions
217 lines (191 loc) · 8.68 KB
/
Copy pathcreate.ps1
File metadata and controls
217 lines (191 loc) · 8.68 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#################################################
# HelloID-Conn-Prov-Target-EmpireDynamics-Create
# PowerShell V2
#################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Resolve-EmpireError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
if (-not [string]::IsNullOrEmpty($ErrorObject.ErrorDetails.Message)) {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
}
elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
if ($null -ne $ErrorObject.Exception.Response) {
$streamReaderResponse = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
if (-not [string]::IsNullOrEmpty($streamReaderResponse)) {
$httpErrorObj.ErrorDetails = $streamReaderResponse
}
}
}
try {
$errorDetailsObject = ($httpErrorObj.ErrorDetails | ConvertFrom-Json)
# Make sure to inspect the error result object and add only the error message as a FriendlyMessage.
# $httpErrorObj.FriendlyMessage = $errorDetailsObject.message
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails # Temporarily assignment
}
catch {
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
}
Write-Output $httpErrorObj
}
}
#endregion
try {
# Create session
$s = New-PSSession -ComputerName $actionContext.Configuration.Applicationserver
# Define variables
$EmailAddress = $actionContext.Data.EmailAddress
$SamAccountName = $actionContext.Data.Username
$UserPrincipalName = $actionContext.Data.userPrincipalName
$Fullname = $actionContext.Data.FullName
$PSPath = $actionContext.Configuration.PSModulePath
$NavServerInstanceName = $actionContext.Configuration.NavServerInstanceName
$tenant = $actionContext.Configuration.Tenant
# Import module
$Import = Invoke-Command -Session $s -ScriptBlock {
param($ImportPath)
Import-Module $ImportPath -ErrorAction Stop
} -ArgumentList $PSPath
# Initial assignment
$outputContext.AccountReference = 'Currently not available'
# Determine action (Create or Correlate)
if ($actionContext.CorrelationConfiguration.Enabled) {
try {
$correlatedAccount = Invoke-Command -Session $s -ScriptBlock {
param($ServerInstance, $Tenant, $UserName)
Get-NAVServerUser -ServerInstance $ServerInstance -Tenant $Tenant |
Where-Object { $_.UserName -eq $UserName }
} -ArgumentList $NavServerInstanceName, $tenant, $SamAccountName
}
catch {
throw "Error while checking for existing NAV user: $($_.Exception.Message)"
}
}
else {
throw "Correlation for this connector is mandatory"
}
if ($null -ne $correlatedAccount) {
$action = 'CorrelateAccount'
}
else {
$action = 'CreateAccount'
}
# Add a message and the result of each of the validations showing what will happen during enforcement
if ($actionContext.DryRun -eq $true) {
Write-Information "[DryRun] $action Authorizationbox account for: [$($personContext.Person.DisplayName)], will be executed during enforcement"
}
switch ($action) {
'CreateAccount' {
Write-Information 'Creating Nav User'
if (-not($actionContext.DryRun -eq $true)) {
#Create user
$createUser = Invoke-Command -Session $s -ScriptBlock {
param(
$ServerInstance,
$Tenant,
$SamAccountName,
$Displayname,
$UserPrincipalName,
$EmailAddress
)
New-NAVServerUser `
-ServerInstance $ServerInstance `
-Tenant $Tenant `
-WindowsAccount $SamAccountName `
-FullName $Displayname `
-AuthenticationEmail $UserPrincipalName `
-ContactEmail $EmailAddress `
-State Enabled `
-Verbose
} -ArgumentList $NavServerInstanceName, $tenant, $SamAccountName, $Fullname, $UserPrincipalName, $EmailAddress
#Set permissions
<#
$setPermissions = Invoke-Command -Session $s -ScriptBlock {
param(
$ServerInstance,
$Tenant,
$SamAccountName,
$PermissionSetId
)
New-NAVServerUserPermissionSet `
-ServerInstance $ServerInstance `
-Tenant $Tenant `
-WindowsAccount $SamAccountName `
-PermissionSetId $PermissionSetId `
-ErrorAction SilentlyContinue `
-Verbose
} -ArgumentList $NavServerInstanceName, $tenant, $SamAccountName, 'SUPER'
#>
#Try another get user for setting aref
$result = Invoke-Command -Session $s -ScriptBlock {
param($ServerInstance, $Tenant, $UserName)
Get-NAVServerUser -ServerInstance $ServerInstance -Tenant $Tenant |
Where-Object { $_.UserName -eq $UserName }
} -ArgumentList $NavServerInstanceName, $tenant, $SamAccountName
$outputContext.Data.UserSecurityID = $result.UserSecurityID
$outputContext.Data.FullName = $result.Fullname
$outputContext.Data.UserName = $result.Username
$auditLogMessage = "Create account was successful. AccountReference is: [$($outputContext.Data.UserSecurityID)]"
}
else {
Write-Information 'Dryrun prevented this action'
}
break
}
'CorrelateAccount' {
Write-Information 'Correlating Nav User'
$outputContext.Data.UserSecurityID = $correlatedAccount.UserSecurityID
$outputContext.Data.Fullname = $correlatedAccount.Fullname
$outputContext.Data.UserName = $correlatedAccount.Username
$outputContext.AccountCorrelated = $true
$auditLogMessage = "Correlated account: [$($actionContext.Data.Username)]. AccountReference is: [$($outputContext.Data.UserSecurityID)]"
break
}
}
$accountRef = [PSCustomObject]@{
UserSecurityID = $outputContext.Data.UserSecurityID
Fullname = $outputContext.Data.Fullname
UserName = $outputContext.Data.UserName
}
$outputContext.AccountReference = $accountRef
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = $action
Message = $auditLogMessage
IsError = $false
})
# Remove session
$exit = if ($s) { Remove-PSSession $s }
}
catch {
$outputContext.Success = $false
$ex = $PSItem
$exit = if ($s) { Remove-PSSession $s }
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-EmpireError -ErrorObject $ex
$auditMessage = "Could not create or correlate EmpireDynamics account. Error: $($errorObj.FriendlyMessage)"
Write-Warning "Error at Line '$($errorObj.ScriptLineNumber)': $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Could not create or correlate EmpireDynamics account. Error: $($ex.Exception.Message)"
Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}