Skip to content

Commit 440f5f2

Browse files
Merge pull request KelvinTegelaar#1645 from PeterVive/intune-policy-listview-customgroupassignment
Allow assignment of intune policies to custom group from table list
2 parents 277788b + f5a08ae commit 440f5f2

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-ExecAssignPolicy.ps1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ Function Invoke-ExecAssignPolicy {
2424

2525
$results = try {
2626
if ($AssignTo) {
27-
$null = Set-CIPPAssignedPolicy -PolicyId $ID -TenantFilter $TenantFilter -GroupName $AssignTo -Type $Type -Headers $Headers
27+
$AssignmentResult = Set-CIPPAssignedPolicy -PolicyId $ID -TenantFilter $TenantFilter -GroupName $AssignTo -Type $Type -Headers $Headers
28+
if ($AssignmentResult) {
29+
# Check if it's a warning message (no groups found)
30+
if ($AssignmentResult -like "*No groups found*") {
31+
$StatusCode = [HttpStatusCode]::BadRequest
32+
} else {
33+
$StatusCode = [HttpStatusCode]::OK
34+
}
35+
$AssignmentResult
36+
} else {
37+
$StatusCode = [HttpStatusCode]::OK
38+
"Successfully edited policy for $($TenantFilter)"
39+
}
40+
} else {
41+
$StatusCode = [HttpStatusCode]::OK
42+
"Successfully edited policy for $($TenantFilter)"
2843
}
29-
"Successfully edited policy for $($TenantFilter)"
30-
$StatusCode = [HttpStatusCode]::OK
3144
} catch {
32-
"Failed to add policy for $($TenantFilter): $($_.Exception.Message)"
3345
$StatusCode = [HttpStatusCode]::InternalServerError
46+
"Failed to add policy for $($TenantFilter): $($_.Exception.Message)"
3447
}
3548

3649

Modules/CIPPCore/Public/Set-CIPPAssignedPolicy.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ function Set-CIPPAssignedPolicy {
5151
)
5252
}
5353
default {
54-
Write-Host "We're supposed to assign a custom group. The group is $GroupName"
5554
$GroupNames = $GroupName.Split(',').Trim()
5655
$GroupIds = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$select=id,displayName&$top=999' -tenantid $TenantFilter |
5756
ForEach-Object {
@@ -61,6 +60,13 @@ function Set-CIPPAssignedPolicy {
6160
}
6261
}
6362
}
63+
64+
if (-not $GroupIds -or $GroupIds.Count -eq 0) {
65+
$ErrorMessage = "No groups found matching the specified name(s): $GroupName. Policy not assigned."
66+
Write-LogMessage -headers $Headers -API $APIName -message $ErrorMessage -Sev 'Warning' -tenant $TenantFilter
67+
return $ErrorMessage
68+
}
69+
6470
foreach ($gid in $GroupIds) {
6571
$assignmentsList.Add(
6672
@{
@@ -102,19 +108,21 @@ function Set-CIPPAssignedPolicy {
102108
}
103109

104110
$AssignJSON = $assignmentsObject | ConvertTo-Json -Depth 10 -Compress
105-
Write-Host "AssignJSON: $AssignJSON"
106111
if ($PSCmdlet.ShouldProcess($GroupName, "Assigning policy $PolicyId")) {
107112
$uri = "https://graph.microsoft.com/beta/$($PlatformType)/$Type('$($PolicyId)')/assign"
108113
$null = New-GraphPOSTRequest -uri $uri -tenantid $TenantFilter -type POST -body $AssignJSON
109114
if ($ExcludeGroup) {
110115
Write-LogMessage -headers $Headers -API $APIName -message "Assigned group '$GroupName' and excluded group '$ExcludeGroup' on Policy $PolicyId" -Sev 'Info' -tenant $TenantFilter
116+
return "Successfully assigned group '$GroupName' and excluded group '$ExcludeGroup' on Policy $PolicyId"
111117
} else {
112118
Write-LogMessage -headers $Headers -API $APIName -message "Assigned group '$GroupName' on Policy $PolicyId" -Sev 'Info' -tenant $TenantFilter
119+
return "Successfully assigned group '$GroupName' on Policy $PolicyId"
113120
}
114121
}
115122

116123
} catch {
117124
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
118125
Write-LogMessage -headers $Headers -API $APIName -message "Failed to assign $GroupName to Policy $PolicyId, using Platform $PlatformType and $Type. The error is:$ErrorMessage" -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
126+
return "Failed to assign $GroupName to Policy $PolicyId. Error: $ErrorMessage"
119127
}
120128
}

0 commit comments

Comments
 (0)