Skip to content

Commit 39abb7e

Browse files
committed
Add error handling to group template parsing
Wrapped group template JSON parsing in a try/catch block to handle malformed data gracefully. Added a default value for missing groupType and improved logging for parse errors.
1 parent d5e86dd commit 39abb7e

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Groups/Invoke-ListGroupTemplates.ps1

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,44 @@ function Invoke-ListGroupTemplates {
1313
$Table = Get-CippTable -tablename 'templates'
1414
$Filter = "PartitionKey eq 'GroupTemplate'"
1515
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
16-
$data = $_.JSON | ConvertFrom-Json -ErrorAction SilentlyContinue
17-
# Normalize groupType to camelCase for consistent frontend handling
18-
# Handle both stored normalized values and legacy values
19-
$normalizedGroupType = switch -Wildcard ($data.groupType) {
20-
# Already normalized values (most common)
21-
'dynamicdistribution' { 'dynamicDistribution'; break }
22-
'azurerole' { 'azureRole'; break }
23-
# Legacy values that might exist in stored templates
24-
'*dynamicdistribution*' { 'dynamicDistribution'; break }
25-
'*dynamic*' { 'dynamic'; break }
26-
'*azurerole*' { 'azureRole'; break }
27-
'*unified*' { 'm365'; break }
28-
'*microsoft*' { 'm365'; break }
29-
'*m365*' { 'm365'; break }
30-
'*generic*' { 'generic'; break }
31-
'*security*' { 'security'; break }
32-
'*distribution*' { 'distribution'; break }
33-
'*mail*' { 'distribution'; break }
34-
default { $data.groupType }
35-
}
16+
try {
17+
$data = $_.JSON | ConvertFrom-Json -ErrorAction SilentlyContinue
18+
# Normalize groupType to camelCase for consistent frontend handling
19+
# Handle both stored normalized values and legacy values
20+
21+
if (!$data.groupType) {
22+
$data.groupType = 'generic'
23+
}
24+
25+
$normalizedGroupType = switch -Wildcard ($data.groupType) {
26+
# Already normalized values (most common)
27+
'dynamicdistribution' { 'dynamicDistribution'; break }
28+
'azurerole' { 'azureRole'; break }
29+
# Legacy values that might exist in stored templates
30+
'*dynamicdistribution*' { 'dynamicDistribution'; break }
31+
'*dynamic*' { 'dynamic'; break }
32+
'*azurerole*' { 'azureRole'; break }
33+
'*unified*' { 'm365'; break }
34+
'*microsoft*' { 'm365'; break }
35+
'*m365*' { 'm365'; break }
36+
'*generic*' { 'generic'; break }
37+
'*security*' { 'security'; break }
38+
'*distribution*' { 'distribution'; break }
39+
'*mail*' { 'distribution'; break }
40+
default { $data.groupType }
41+
}
3642

37-
[PSCustomObject]@{
38-
displayName = $data.displayName
39-
description = $data.description
40-
groupType = $normalizedGroupType
41-
membershipRules = $data.membershipRules
42-
allowExternal = $data.allowExternal
43-
username = $data.username
44-
GUID = $_.RowKey
43+
[PSCustomObject]@{
44+
displayName = $data.displayName
45+
description = $data.description
46+
groupType = $normalizedGroupType
47+
membershipRules = $data.membershipRules
48+
allowExternal = $data.allowExternal
49+
username = $data.username
50+
GUID = $_.RowKey
51+
}
52+
} catch {
53+
Write-Information "Could not parse group template $($_.RowKey): $($_.Exception.Message)"
4554
}
4655
} | Sort-Object -Property displayName
4756

0 commit comments

Comments
 (0)