Skip to content

Commit 10a23c7

Browse files
Merge pull request KelvinTegelaar#1454 from kris6673/add-team-modernize
Feat: Improve error handling and logging in team creation
2 parents 8ea4175 + e3ad53e commit 10a23c7

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ExecGroupsDelete.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Function Invoke-ExecGroupsDelete {
2121
$DisplayName = $Request.Query.displayName ?? $Request.Body.displayName
2222

2323
Try {
24-
$Result = Remove-CIPPGroup -ID $ID -Grouptype $GroupType -TenantFilter $TenantFilter -DisplayName $DisplayName -APIName $APIName -Headers $Headers
24+
$Result = Remove-CIPPGroup -ID $ID -GroupType $GroupType -TenantFilter $TenantFilter -DisplayName $DisplayName -APIName $APIName -Headers $Headers
2525
$StatusCode = [HttpStatusCode]::OK
2626
} catch {
2727
$Result = "$($_.Exception.Message)"

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Teams-Sharepoint/Invoke-AddTeam.ps1

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ Function Invoke-AddTeam {
1414
$Headers = $Request.Headers
1515
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
1616

17-
$userobj = $Request.body
17+
# Interact with the body of the request
18+
$TeamObj = $Request.Body
19+
$TenantID = $TeamObj.tenantid
1820

19-
20-
21-
$Owners = ($userobj.owner)
21+
$Owners = ($TeamObj.owner)
2222
try {
2323
if ($null -eq $Owners) {
24-
throw "You have to add at least one owner to the team"
24+
throw 'You have to add at least one owner to the team'
2525
}
2626
$Owners = $Owners | ForEach-Object {
2727
$OwnerID = "https://graph.microsoft.com/beta/users('$($_)')"
@@ -34,28 +34,31 @@ Function Invoke-AddTeam {
3434

3535
$TeamsSettings = [PSCustomObject]@{
3636
'template@odata.bind' = "https://graph.microsoft.com/v1.0/teamsTemplates('standard')"
37-
'visibility' = $userobj.visibility
38-
'displayName' = $userobj.displayname
39-
'description' = $userobj.description
40-
'members' = @($owners)
37+
'visibility' = $TeamObj.visibility
38+
'displayName' = $TeamObj.displayName
39+
'description' = $TeamObj.description
40+
'members' = @($Owners)
4141

4242
} | ConvertTo-Json -Depth 10
43+
# Write-Host $TeamsSettings
4344

44-
Write-Host $TeamsSettings
45-
New-GraphPostRequest -AsApp $true -uri 'https://graph.microsoft.com/beta/teams' -tenantid $Userobj.tenantid -type POST -body $TeamsSettings -verbose
46-
Write-LogMessage -headers $Request.Headers -API $APINAME -tenant $($userobj.tenantid) -message "Added Team $($userobj.displayname)" -Sev 'Info'
47-
$body = [pscustomobject]@{'Results' = 'Success. Team has been added' }
45+
$null = New-GraphPostRequest -AsApp $true -uri 'https://graph.microsoft.com/beta/teams' -tenantid $TenantID -type POST -body $TeamsSettings -Verbose
46+
$Message = "Successfully created Team: '$($TeamObj.displayName)'"
47+
Write-LogMessage -headers $Headers -API $APINAME -tenant $TenantID -message $Message -Sev Info
48+
$StatusCode = [HttpStatusCode]::OK
4849

4950
} catch {
50-
Write-LogMessage -headers $Request.Headers -API $APINAME -tenant $($userobj.tenantid) -message "Adding Team failed. Error: $($_.Exception.Message)" -Sev 'Error'
51-
$body = [pscustomobject]@{'Results' = "Failed. Error message: $($_.Exception.Message)" }
51+
$ErrorMessage = Get-CippException -Exception $_
52+
$Message = "Failed to create Team: '$($TeamObj.displayName)'. Error: $($ErrorMessage.NormalizedError)"
53+
Write-LogMessage -headers $Headers -API $APINAME -tenant $TenantID -message $Message -Sev Error -LogData $ErrorMessage
54+
$StatusCode = [HttpStatusCode]::InternalServerError
5255
}
5356

5457

5558
# Associate values to output bindings by calling 'Push-OutputBinding'.
5659
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
57-
StatusCode = [HttpStatusCode]::OK
58-
Body = $Body
60+
StatusCode = $StatusCode
61+
Body = @{ Results = $Message }
5962
})
6063

6164
}

0 commit comments

Comments
 (0)