@@ -6,11 +6,36 @@ function Set-CIPPAssignedApplication {
66 $AppType ,
77 $ApplicationId ,
88 $TenantFilter ,
9+ $GroupIds ,
10+ $AssignmentMode = ' replace' ,
911 $APIName = ' Assign Application' ,
1012 $Headers
1113 )
1214 Write-Host " GroupName: $GroupName Intent: $Intent AppType: $AppType ApplicationId: $ApplicationId TenantFilter: $TenantFilter APIName: $APIName "
1315 try {
16+ $assignmentSettings = $null
17+ if ($AppType ) {
18+ $assignmentSettings = @ {
19+ ' @odata.type' = " #microsoft.graph.$ ( $AppType ) AppAssignmentSettings"
20+ }
21+
22+ switch ($AppType ) {
23+ ' Win32Lob' {
24+ $assignmentSettings.notifications = ' hideAll'
25+ }
26+ ' WinGet' {
27+ $assignmentSettings.notifications = ' hideAll'
28+ }
29+ ' macOsVpp' {
30+ $assignmentSettings.useDeviceLicensing = $true
31+ }
32+ default {
33+ # No additional settings
34+ }
35+ }
36+ }
37+
38+ # Build the assignment object
1439 $MobileAppAssignment = switch ($GroupName ) {
1540 ' AllUsers' {
1641 @ (@ {
@@ -19,12 +44,7 @@ function Set-CIPPAssignedApplication {
1944 ' @odata.type' = ' #microsoft.graph.allLicensedUsersAssignmentTarget'
2045 }
2146 intent = $Intent
22- settings = @ {
23- ' @odata.type' = " #microsoft.graph.$ ( $appType ) AppAssignmentSettings"
24- notifications = ' hideAll'
25- installTimeSettings = $null
26- restartSettings = $null
27- }
47+ settings = $assignmentSettings
2848 })
2949 break
3050 }
@@ -35,12 +55,7 @@ function Set-CIPPAssignedApplication {
3555 ' @odata.type' = ' #microsoft.graph.allDevicesAssignmentTarget'
3656 }
3757 intent = $Intent
38- settings = @ {
39- ' @odata.type' = " #microsoft.graph.$ ( $appType ) AppAssignmentSettings"
40- notifications = ' hideAll'
41- installTimeSettings = $null
42- restartSettings = $null
43- }
58+ settings = $assignmentSettings
4459 })
4560 break
4661 }
@@ -52,71 +67,121 @@ function Set-CIPPAssignedApplication {
5267 ' @odata.type' = ' #microsoft.graph.allLicensedUsersAssignmentTarget'
5368 }
5469 intent = $Intent
55- settings = @ {
56- ' @odata.type' = " #microsoft.graph.$ ( $appType ) AppAssignmentSettings"
57- notifications = ' hideAll'
58- installTimeSettings = $null
59- restartSettings = $null
60- }
70+ settings = $assignmentSettings
6171 },
6272 @ {
6373 ' @odata.type' = ' #microsoft.graph.mobileAppAssignment'
6474 target = @ {
6575 ' @odata.type' = ' #microsoft.graph.allDevicesAssignmentTarget'
6676 }
6777 intent = $Intent
68- settings = @ {
69- ' @odata.type' = " #microsoft.graph.$ ( $appType ) AppAssignmentSettings"
70- notifications = ' hideAll'
71- installTimeSettings = $null
72- restartSettings = $null
73- }
78+ settings = $assignmentSettings
7479 }
7580 )
7681 }
7782 default {
78- $GroupNames = $GroupName.Split (' ,' )
79- $GroupIds = New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/groups' - tenantid $TenantFilter | ForEach-Object {
80- $Group = $_
81- foreach ($SingleName in $GroupNames ) {
82- if ($_.displayname -like $SingleName ) {
83- $group.id
83+ $resolvedGroupIds = @ ()
84+ if ($PSBoundParameters.ContainsKey (' GroupIds' ) -and $GroupIds ) {
85+ $resolvedGroupIds = $GroupIds
86+ } else {
87+ $GroupNames = $GroupName.Split (' ,' )
88+ $resolvedGroupIds = New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/groups' - tenantid $TenantFilter | ForEach-Object {
89+ $Group = $_
90+ foreach ($SingleName in $GroupNames ) {
91+ if ($_.displayName -like $SingleName ) {
92+ $group.id
93+ }
8494 }
8595 }
96+ Write-Information " found $ ( $resolvedGroupIds ) groups"
8697 }
87- Write-Information " found $ ( $GroupIds ) groups"
88- foreach ($Group in $GroupIds ) {
98+
99+ # We ain't found nothing so we panic
100+ if (-not $resolvedGroupIds ) {
101+ throw ' No matching groups resolved for assignment request.'
102+ }
103+
104+ foreach ($Group in $resolvedGroupIds ) {
89105 @ {
90106 ' @odata.type' = ' #microsoft.graph.mobileAppAssignment'
91107 target = @ {
92108 ' @odata.type' = ' #microsoft.graph.groupAssignmentTarget'
93109 groupId = $Group
94110 }
95111 intent = $Intent
96- settings = @ {
97- ' @odata.type' = " #microsoft.graph.$ ( $appType ) AppAssignmentSettings"
98- notifications = ' hideAll'
99- installTimeSettings = $null
100- restartSettings = $null
112+ settings = $assignmentSettings
113+ }
114+ }
115+ }
116+ }
117+
118+ # If we're appending, we need to get existing assignments
119+ if ($AssignmentMode -eq ' append' ) {
120+ try {
121+ $ExistingAssignments = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/$ ( $ApplicationId ) /assignments" - tenantid $TenantFilter
122+ } catch {
123+ Write-Warning " Unable to retrieve existing assignments for $ApplicationId . Proceeding with new assignments only. Error: $ ( $_.Exception.Message ) "
124+ $ExistingAssignments = @ ()
125+ }
126+ }
127+
128+ # Deduplicate current assignments so the new ones override existing ones
129+ if ($ExistingAssignments ) {
130+ $ExistingAssignments = $ExistingAssignments | ForEach-Object {
131+ $ExistingAssignment = $_
132+ switch ($ExistingAssignment.target .' @odata.type' ) {
133+ ' #microsoft.graph.groupAssignmentTarget' {
134+ if ($ExistingAssignment.target.groupId -notin $MobileAppAssignment.target.groupId ) {
135+ $ExistingAssignment
136+ }
137+ }
138+ default {
139+ if ($ExistingAssignment.target .' @odata.type' -notin $MobileAppAssignment.target .' @odata.type' ) {
140+ $ExistingAssignment
101141 }
102142 }
103143 }
104144 }
105145 }
146+
147+ $FinalAssignments = [System.Collections.Generic.List [object ]]::new()
148+ if ($AssignmentMode -eq ' append' -and $ExistingAssignments ) {
149+ $ExistingAssignments | ForEach-Object {
150+ $FinalAssignments.Add (@ {
151+ ' @odata.type' = ' #microsoft.graph.mobileAppAssignment'
152+ target = $_.target
153+ intent = $_.intent
154+ settings = $_.settings
155+ })
156+ }
157+
158+ $MobileAppAssignment | ForEach-Object {
159+ $FinalAssignments.Add (@ {
160+ ' @odata.type' = ' #microsoft.graph.mobileAppAssignment'
161+ target = $_.target
162+ intent = $_.intent
163+ settings = $_.settings
164+ })
165+ }
166+ } else {
167+ $FinalAssignments = $MobileAppAssignment
168+ }
169+
106170 $DefaultAssignmentObject = [PSCustomObject ]@ {
107171 mobileAppAssignments = @ (
108- $MobileAppAssignment
172+ $FinalAssignments
109173 )
110174 }
111175 if ($PSCmdlet.ShouldProcess ($GroupName , " Assigning Application $ApplicationId " )) {
112176 Start-Sleep - Seconds 1
177+ # Write-Information (ConvertTo-Json $DefaultAssignmentObject -Depth 10)
113178 $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/$ ( $ApplicationId ) /assign" - tenantid $TenantFilter - type POST - body ($DefaultAssignmentObject | ConvertTo-Json - Compress - Depth 10 )
114- Write-LogMessage - headers $Headers - API $APIName - message " Assigned Application to $ ( $GroupName ) " - Sev ' Info' - tenant $TenantFilter
179+ Write-LogMessage - headers $Headers - API $APIName - message " Assigned Application $ApplicationId to $ ( $GroupName ) " - Sev ' Info' - tenant $TenantFilter
115180 }
116- return " Assigned Application to $ ( $GroupName ) "
181+ return " Assigned Application $ApplicationId to $ ( $GroupName ) "
117182 } catch {
118183 $ErrorMessage = Get-CippException - Exception $_
119- Write-LogMessage - headers $Headers - API $APIName - message " Could not assign application to $GroupName . Error: $ ( $ErrorMessage.NormalizedError ) " - Sev ' Error' - tenant $TenantFilter - LogData $ErrorMessage
120- return " Could not assign application to $GroupName . Error: $ ( $ErrorMessage.NormalizedError ) "
184+ Write-LogMessage - headers $Headers - API $APIName - message " Could not assign application $ApplicationId to $GroupName . Error: $ ( $ErrorMessage.NormalizedError ) " - Sev ' Error' - tenant $TenantFilter - LogData $ErrorMessage
185+ throw " Could not assign application $ApplicationId to $GroupName . Error: $ ( $ErrorMessage.NormalizedError ) "
121186 }
122187}
0 commit comments