@@ -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