@@ -11,80 +11,76 @@ Function Invoke-AddGroup {
1111 param ($Request , $TriggerMetadata )
1212
1313 $APIName = $Request.Params.CIPPEndpoint
14- Write-LogMessage - headers $Request.Headers - API $APINAME - message ' Accessed this API' - Sev ' Debug'
14+ $SelectedTenants = if (' AllTenants' -in $SelectedTenants ) { (Get-Tenants ).defaultDomainName } else { $Request.body.tenantFilter.value ? $Request.body.tenantFilter.value : $Request.body.tenantFilter }
15+ Write-LogMessage - headers $Request.Headers - API $APIName - message ' Accessed this API' - Sev Debug
1516
16- $groupobj = $Request.body
17- $SelectedTenants = $request.body.tenantfilter.value ? $request.body.tenantfilter.value : $request.body.tenantfilter
18- if (' AllTenants' -in $SelectedTenants ) { $SelectedTenants = (Get-Tenants ).defaultDomainName }
1917
20- # Write to the Azure Functions log stream.
21- Write-Host ' PowerShell HTTP trigger function processed a request. '
22- $results = foreach ($tenant in $SelectedTenants ) {
18+ $GroupObject = $Request .body
19+
20+ $Results = foreach ($tenant in $SelectedTenants ) {
2321 try {
24- $email = if ($groupobj .primDomain.value ) { " $ ( $groupobj .username ) @$ ( $groupobj .primDomain.value ) " } else { " $ ( $groupobj .username ) @$ ( $tenant ) " }
25- if ($groupobj .groupType -in ' Generic' , ' azurerole' , ' dynamic' , ' m365' ) {
22+ $Email = if ($GroupObject .primDomain.value ) { " $ ( $GroupObject .username ) @$ ( $GroupObject .primDomain.value ) " } else { " $ ( $GroupObject .username ) @$ ( $tenant ) " }
23+ if ($GroupObject .groupType -in ' Generic' , ' azurerole' , ' dynamic' , ' m365' ) {
2624
27- $BodyToship = [pscustomobject ] @ {
28- ' displayName' = $groupobj .Displayname
29- ' description' = $groupobj .Description
30- ' mailNickname' = $groupobj .username
25+ $BodyParams = [pscustomobject ] @ {
26+ ' displayName' = $GroupObject .displayName
27+ ' description' = $GroupObject .description
28+ ' mailNickname' = $GroupObject .username
3129 mailEnabled = [bool ]$false
3230 securityEnabled = [bool ]$true
33- isAssignableToRole = [bool ]($groupobj | Where-Object - Property groupType -EQ ' AzureRole' )
31+ isAssignableToRole = [bool ]($GroupObject | Where-Object - Property groupType -EQ ' AzureRole' )
3432 }
35- if ($groupobj .membershipRules ) {
36- $BodyToship | Add-Member - NotePropertyName ' membershipRule' - NotePropertyValue ($groupobj .membershipRules )
37- $BodyToship | Add-Member - NotePropertyName ' groupTypes' - NotePropertyValue @ (' DynamicMembership' )
38- $BodyToship | Add-Member - NotePropertyName ' membershipRuleProcessingState' - NotePropertyValue ' On'
33+ if ($GroupObject .membershipRules ) {
34+ $BodyParams | Add-Member - NotePropertyName ' membershipRule' - NotePropertyValue ($GroupObject .membershipRules )
35+ $BodyParams | Add-Member - NotePropertyName ' groupTypes' - NotePropertyValue @ (' DynamicMembership' )
36+ $BodyParams | Add-Member - NotePropertyName ' membershipRuleProcessingState' - NotePropertyValue ' On'
3937 }
40- if ($groupobj .groupType -eq ' m365' ) {
41- $BodyToship | Add-Member - NotePropertyName ' groupTypes' - NotePropertyValue @ (' Unified' )
38+ if ($GroupObject .groupType -eq ' m365' ) {
39+ $BodyParams | Add-Member - NotePropertyName ' groupTypes' - NotePropertyValue @ (' Unified' )
4240 }
43- if ($groupobj .owners -AND $groupobj .groupType -in ' generic' , ' azurerole' , ' security' ) {
44- $BodyToship | Add-Member - NotePropertyName ' owners@odata.bind' - NotePropertyValue (($groupobj .AddOwner ) | ForEach-Object { " https://graph.microsoft.com/v1.0/users/$ ( $_.value ) " })
45- $bodytoship .' owners@odata.bind' = @ ($bodytoship .' owners@odata.bind' )
41+ if ($GroupObject .owners -AND $GroupObject .groupType -in ' generic' , ' azurerole' , ' security' ) {
42+ $BodyParams | Add-Member - NotePropertyName ' owners@odata.bind' - NotePropertyValue (($GroupObject .AddOwner ) | ForEach-Object { " https://graph.microsoft.com/v1.0/users/$ ( $_.value ) " })
43+ $BodyParams .' owners@odata.bind' = @ ($BodyParams .' owners@odata.bind' )
4644 }
47- if ($groupobj .members -AND $groupobj .groupType -in ' generic' , ' azurerole' , ' security' ) {
48- $BodyToship | Add-Member - NotePropertyName ' members@odata.bind' - NotePropertyValue (($groupobj .AddMember ) | ForEach-Object { " https://graph.microsoft.com/v1.0/users/$ ( $_.value ) " })
49- $BodyToship .' members@odata.bind' = @ ($BodyToship .' members@odata.bind' )
45+ if ($GroupObject .members -AND $GroupObject .groupType -in ' generic' , ' azurerole' , ' security' ) {
46+ $BodyParams | Add-Member - NotePropertyName ' members@odata.bind' - NotePropertyValue (($GroupObject .AddMember ) | ForEach-Object { " https://graph.microsoft.com/v1.0/users/$ ( $_.value ) " })
47+ $BodyParams .' members@odata.bind' = @ ($BodyParams .' members@odata.bind' )
5048 }
51- $GraphRequest = New-GraphPostRequest - uri ' https://graph.microsoft.com/beta/groups' - tenantid $tenant - type POST - body (ConvertTo-Json - InputObject $BodyToship - Depth 10 ) - verbose
49+ $GraphRequest = New-GraphPostRequest - uri ' https://graph.microsoft.com/beta/groups' - tenantid $tenant - type POST - body (ConvertTo-Json - InputObject $BodyParams - Depth 10 ) - Verbose
5250 } else {
53- if ($groupobj .groupType -eq ' dynamicdistribution ' ) {
54- $Params = @ {
55- Name = $groupobj .Displayname
56- RecipientFilter = $groupobj .membershipRules
57- PrimarySmtpAddress = $email
51+ if ($GroupObject .groupType -eq ' dynamicDistribution ' ) {
52+ $ExoParams = @ {
53+ Name = $GroupObject .displayName
54+ RecipientFilter = $GroupObject .membershipRules
55+ PrimarySmtpAddress = $Email
5856 }
59- $GraphRequest = New-ExoRequest - tenantid $tenant - cmdlet ' New-DynamicDistributionGroup' - cmdParams $params
57+ $GraphRequest = New-ExoRequest - tenantid $tenant - cmdlet ' New-DynamicDistributionGroup' - cmdParams $ExoParams
6058 } else {
61- $Params = @ {
62- Name = $groupobj .Displayname
63- Alias = $groupobj .username
64- Description = $groupobj .Description
65- PrimarySmtpAddress = $email
66- Type = $groupobj .groupType
67- RequireSenderAuthenticationEnabled = [bool ]! $groupobj .AllowExternal
59+ $ExoParams = @ {
60+ Name = $GroupObject .displayName
61+ Alias = $GroupObject .username
62+ Description = $GroupObject .description
63+ PrimarySmtpAddress = $Email
64+ Type = $GroupObject .groupType
65+ RequireSenderAuthenticationEnabled = [bool ]! $GroupObject .allowExternal
6866 }
69- $GraphRequest = New-ExoRequest - tenantid $tenant - cmdlet ' New-DistributionGroup' - cmdParams $params
67+ $GraphRequest = New-ExoRequest - tenantid $tenant - cmdlet ' New-DistributionGroup' - cmdParams $ExoParams
7068 }
71- # $GraphRequest = New-ExoRequest -tenantid $tenant -cmdlet 'New-DistributionGroup' -cmdParams $params
72- # At some point add logic to use AddOwner/AddMember for New-DistributionGroup, but idk how we're going to brr that - rvdwegen
7369 }
74- " Successfully created group $ ( $groupobj .displayname ) for $ ( $tenant ) "
75- Write-LogMessage - headers $Request.Headers - API $APINAME - tenant $tenant - message " Created group $ ( $groupobj .displayname ) with id $ ( $GraphRequest.id ) " - Sev ' Info'
70+ " Successfully created group $ ( $GroupObject .displayName ) for $ ( $tenant ) "
71+ Write-LogMessage - headers $Request.Headers - API $APIName - tenant $tenant - message " Created group $ ( $GroupObject .displayName ) with id $ ( $GraphRequest.id ) " - Sev Info
7672
7773 } catch {
78- Write-LogMessage - headers $Request.Headers - API $APINAME - tenant $tenant - message " Group creation API failed. $ ( $_.Exception.Message ) " - Sev ' Error'
79- " Failed to create group. $ ( $groupobj.displayname ) for $ ( $tenant ) $ ( $_.Exception.Message ) "
74+ $ErrorMessage = Get-CippException - Exception $_
75+ Write-LogMessage - headers $Request.Headers - API $APIName - tenant $tenant - message " Group creation API failed. $ ( $ErrorMessage.NormalizedError ) " - Sev Error - LogData $ErrorMessage
76+ " Failed to create group. $ ( $GroupObject.displayName ) for $ ( $tenant ) $ ( $ErrorMessage.NormalizedError ) "
8077 }
8178 }
82- $body = [pscustomobject ]@ {' Results' = @ ($results ) }
79+ $ResponseBody = [pscustomobject ]@ {' Results' = @ ($Results ) }
8380
8481 # Associate values to output bindings by calling 'Push-OutputBinding'.
8582 Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
8683 StatusCode = [HttpStatusCode ]::OK
87- Body = $Body
84+ Body = $ResponseBody
8885 })
89-
9086}
0 commit comments