@@ -27,48 +27,62 @@ function Invoke-AddUser {
2727 }
2828
2929 if ($UserObj.Scheduled.Enabled ) {
30- $Username = $UserObj.username ?? $UserObj.mailNickname
31- $TaskBody = [pscustomobject ]@ {
32- TenantFilter = $UserObj.tenantFilter
33- Name = " New user creation: $ ( $Username ) @$ ( $UserObj.PrimDomain.value ) "
34- Command = @ {
35- value = ' New-CIPPUserTask'
36- label = ' New-CIPPUserTask'
30+ try {
31+ $Username = $UserObj.username ?? $UserObj.mailNickname
32+ $TaskBody = [pscustomobject ]@ {
33+ TenantFilter = $UserObj.tenantFilter
34+ Name = " New user creation: $ ( $Username ) @$ ( $UserObj.PrimDomain.value ) "
35+ Command = @ {
36+ value = ' New-CIPPUserTask'
37+ label = ' New-CIPPUserTask'
38+ }
39+ Parameters = [pscustomobject ]@ { UserObj = $UserObj }
40+ ScheduledTime = $UserObj.Scheduled.date
41+ Reference = $UserObj.reference ?? $null
42+ PostExecution = @ {
43+ Webhook = [bool ]$Request.Body.PostExecution.Webhook
44+ Email = [bool ]$Request.Body.PostExecution.Email
45+ PSA = [bool ]$Request.Body.PostExecution.PSA
46+ }
3747 }
38- Parameters = [pscustomobject ]@ { UserObj = $UserObj }
39- ScheduledTime = $UserObj.Scheduled.date
40- Reference = $UserObj.reference ?? $null
41- PostExecution = @ {
42- Webhook = [bool ]$Request.Body.PostExecution.Webhook
43- Email = [bool ]$Request.Body.PostExecution.Email
44- PSA = [bool ]$Request.Body.PostExecution.PSA
48+ Add-CIPPScheduledTask - Task $TaskBody - hidden $false - DisallowDuplicateName $true - Headers $Headers
49+ $body = [pscustomobject ] @ {
50+ ' Results' = @ (" Successfully created scheduled task to create user $ ( $UserObj.DisplayName ) " )
4551 }
46- }
47- Add-CIPPScheduledTask - Task $TaskBody - hidden $false - DisallowDuplicateName $true - Headers $Headers
48- $body = [pscustomobject ] @ {
49- ' Results' = @ (" Successfully created scheduled task to create user $ ( $UserObj.DisplayName ) " )
52+ } catch {
53+ $body = [pscustomobject ] @ {
54+ ' Results' = @ (" Failed to create scheduled task to create user $ ( $UserObj.DisplayName ) : $ ( $_.Exception.Message ) " )
55+ }
56+ $StatusCode = [HttpStatusCode ]::InternalServerError
5057 }
5158 } else {
52- $CreationResults = New-CIPPUserTask - UserObj $UserObj - APIName $APIName - Headers $Headers
53- $body = [pscustomobject ] @ {
54- ' Results' = @ (
55- $CreationResults.Results [0 ],
56- $CreationResults.Results [1 ],
57- @ {
58- ' resultText' = $CreationResults.Results [2 ]
59- ' copyField' = $CreationResults.password
60- ' state' = ' success'
59+ try {
60+ $CreationResults = New-CIPPUserTask - UserObj $UserObj - APIName $APIName - Headers $Headers
61+ $body = [pscustomobject ] @ {
62+ ' Results' = @ (
63+ $CreationResults.Results [0 ],
64+ $CreationResults.Results [1 ],
65+ @ {
66+ ' resultText' = $CreationResults.Results [2 ]
67+ ' copyField' = $CreationResults.password
68+ ' state' = ' success'
69+ }
70+ )
71+ ' CopyFrom' = @ {
72+ ' Success' = $CreationResults.CopyFrom.Success
73+ ' Error' = $CreationResults.CopyFrom.Error
6174 }
62- )
63- ' CopyFrom' = @ {
64- ' Success' = $CreationResults.CopyFrom.Success
65- ' Error' = $CreationResults.CopyFrom.Error
75+ ' User' = $CreationResults.User
76+ }
77+ } catch {
78+ $body = [pscustomobject ] @ {
79+ ' Results' = @ (" $ ( $_.Exception.Message ) " )
6680 }
67- ' User ' = $CreationResults .User
81+ $StatusCode = [ HttpStatusCode ]::InternalServerError
6882 }
6983 }
7084 return ([HttpResponseContext ]@ {
71- StatusCode = [HttpStatusCode ]::OK
85+ StatusCode = $StatusCode ? $StatusCode : [HttpStatusCode ]::OK
7286 Body = $Body
7387 })
7488
0 commit comments