@@ -11,7 +11,157 @@ function Invoke-ListTenantGroups {
1111 param ($Request , $TriggerMetadata )
1212
1313 $groupFilter = $Request.Query.groupId ?? $Request.Body.groupId
14+ $includeUsage = $Request.Query.includeUsage ?? $Request.Body.includeUsage
1415 $TenantGroups = (Get-TenantGroups - GroupId $groupFilter - SkipCache) ?? @ ()
16+
17+ if ($includeUsage -eq ' true' ) {
18+ $UsageByGroup = @ {}
19+ foreach ($Group in $TenantGroups ) {
20+ $UsageByGroup [$Group.Id ] = [System.Collections.Generic.List [PSCustomObject ]]::new()
21+ }
22+
23+ $AddGroupUsage = {
24+ param ($FilterArray , $UsedIn , $Name , $Type )
25+ foreach ($Filter in $FilterArray ) {
26+ if ($Filter.type -eq ' Group' -and $Filter.value -and $UsageByGroup.ContainsKey ($Filter.value )) {
27+ $UsageByGroup [$Filter.value ].Add([PSCustomObject ]@ {
28+ UsedIn = $UsedIn
29+ Name = $Name
30+ Type = $Type
31+ })
32+ }
33+ }
34+ }
35+
36+ # Standards Templates
37+ $TemplateTable = Get-CippTable - tablename ' templates'
38+ $TemplateFilter = " PartitionKey eq 'StandardsTemplateV2'"
39+ $Templates = Get-CIPPAzDataTableEntity @TemplateTable - Filter $TemplateFilter
40+
41+ foreach ($Template in $Templates ) {
42+ try {
43+ $TemplateData = $Template.JSON | ConvertFrom-Json
44+ $TemplateName = $TemplateData.templateName ?? $Template.RowKey
45+ if ($TemplateData.tenantFilter ) {
46+ & $AddGroupUsage $TemplateData.tenantFilter ' Standards Template' $TemplateName ' Tenant Filter'
47+ }
48+ if ($TemplateData.excludedTenants ) {
49+ & $AddGroupUsage $TemplateData.excludedTenants ' Standards Template' $TemplateName ' Excluded Tenants'
50+ }
51+ } catch {
52+ Write-Warning " Failed to parse standards template $ ( $Template.RowKey ) : $ ( $_.Exception.Message ) "
53+ }
54+ }
55+
56+ # Scheduled Tasks
57+ $TaskTable = Get-CippTable - tablename ' ScheduledTasks'
58+ $TaskFilter = " PartitionKey eq 'ScheduledTask'"
59+ $Tasks = Get-CIPPAzDataTableEntity @TaskTable - Filter $TaskFilter
60+
61+ foreach ($Task in $Tasks ) {
62+ if ($Task.TenantGroup ) {
63+ try {
64+ $TenantGroupObject = $Task.TenantGroup | ConvertFrom-Json - ErrorAction SilentlyContinue
65+ if ($TenantGroupObject.value -and $UsageByGroup.ContainsKey ($TenantGroupObject.value )) {
66+ $UsageByGroup [$TenantGroupObject.value ].Add([PSCustomObject ]@ {
67+ UsedIn = ' Scheduled Task'
68+ Name = $Task.Name ?? $Task.RowKey
69+ Type = ' Tenant Filter'
70+ })
71+ }
72+ } catch {
73+ Write-Warning " Failed to parse tenant group for task $ ( $Task.RowKey ) : $ ( $_.Exception.Message ) "
74+ }
75+ }
76+ }
77+
78+ # Dynamic Group Rules referencing other groups
79+ foreach ($Group in $TenantGroups ) {
80+ if ($Group.GroupType -eq ' dynamic' -and $Group.DynamicRules ) {
81+ foreach ($Rule in $Group.DynamicRules ) {
82+ if ($Rule.property -eq ' TenantGroup' -and $Rule.value -and $UsageByGroup.ContainsKey ($Rule.value )) {
83+ $UsageByGroup [$Rule.value ].Add([PSCustomObject ]@ {
84+ UsedIn = ' Dynamic Group Rule'
85+ Name = $Group.Name
86+ Type = ' Rule Reference'
87+ })
88+ }
89+ }
90+ }
91+ }
92+
93+ # Webhook Rules
94+ $WebhookTable = Get-CippTable - tablename ' WebhookRules'
95+ $WebhookRules = Get-CIPPAzDataTableEntity @WebhookTable
96+
97+ foreach ($Rule in $WebhookRules ) {
98+ try {
99+ $RuleName = $Rule.Name ?? $Rule.RowKey
100+ if ($Rule.Tenants ) {
101+ $Tenants = $Rule.Tenants | ConvertFrom-Json - ErrorAction SilentlyContinue
102+ if ($Tenants ) {
103+ & $AddGroupUsage $Tenants ' Alert Rule' $RuleName ' Tenant Filter'
104+ }
105+ }
106+ if ($Rule.excludedTenants ) {
107+ $ExclTenants = $Rule.excludedTenants | ConvertFrom-Json - ErrorAction SilentlyContinue
108+ if ($ExclTenants ) {
109+ & $AddGroupUsage $ExclTenants ' Alert Rule' $RuleName ' Excluded Tenants'
110+ }
111+ }
112+ } catch {
113+ Write-Warning " Failed to parse webhook rule $ ( $Rule.RowKey ) : $ ( $_.Exception.Message ) "
114+ }
115+ }
116+
117+ # Custom Roles
118+ $RolesTable = Get-CippTable - tablename ' CustomRoles'
119+ $CustomRoles = Get-CIPPAzDataTableEntity @RolesTable
120+
121+ foreach ($Role in $CustomRoles ) {
122+ try {
123+ $RoleName = $Role.Name ?? $Role.RowKey
124+ if ($Role.AllowedTenants ) {
125+ $AllowedTenants = $Role.AllowedTenants | ConvertFrom-Json - ErrorAction SilentlyContinue
126+ if ($AllowedTenants ) {
127+ & $AddGroupUsage $AllowedTenants ' Custom Role' $RoleName ' Allowed Tenants'
128+ }
129+ }
130+ if ($Role.BlockedTenants ) {
131+ $BlockedTenants = $Role.BlockedTenants | ConvertFrom-Json - ErrorAction SilentlyContinue
132+ if ($BlockedTenants ) {
133+ & $AddGroupUsage $BlockedTenants ' Custom Role' $RoleName ' Blocked Tenants'
134+ }
135+ }
136+ } catch {
137+ Write-Warning " Failed to parse custom role $ ( $Role.RowKey ) : $ ( $_.Exception.Message ) "
138+ }
139+ }
140+
141+ # Custom Data Mappings
142+ $MappingsTable = Get-CippTable - tablename ' CustomDataMappings'
143+ $Mappings = Get-CIPPAzDataTableEntity @MappingsTable
144+
145+ foreach ($Mapping in $Mappings ) {
146+ try {
147+ $MappingName = $Mapping.Name ?? $Mapping.RowKey
148+ if ($Mapping.tenantFilter ) {
149+ $TenantFilters = $Mapping.tenantFilter | ConvertFrom-Json - ErrorAction SilentlyContinue
150+ if ($TenantFilters ) {
151+ if ($TenantFilters -isnot [System.Array ]) { $TenantFilters = @ ($TenantFilters ) }
152+ & $AddGroupUsage $TenantFilters ' Data Mapping' $MappingName ' Tenant Filter'
153+ }
154+ }
155+ } catch {
156+ Write-Warning " Failed to parse custom data mapping $ ( $Mapping.RowKey ) : $ ( $_.Exception.Message ) "
157+ }
158+ }
159+
160+ foreach ($Group in $TenantGroups ) {
161+ $Group | Add-Member - MemberType NoteProperty - Name ' Usage' - Value @ ($UsageByGroup [$Group.Id ]) - Force
162+ }
163+ }
164+
15165 $Body = @ { Results = @ ($TenantGroups ) }
16166
17167 return ([HttpResponseContext ]@ {
0 commit comments