Skip to content

Commit 293552f

Browse files
updates to logic
1 parent 3061a75 commit 293552f

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function Invoke-ExecCreateDefaultGroups {
2+
<#
3+
.SYNOPSIS
4+
Create default tenant groups
5+
.DESCRIPTION
6+
This function creates a set of default tenant groups that are commonly used
7+
.FUNCTIONALITY
8+
Entrypoint,AnyTenant
9+
.ROLE
10+
Tenant.Groups.ReadWrite
11+
#>
12+
[CmdletBinding()]
13+
param($Request, $TriggerMetadata)
14+
15+
try {
16+
$Table = Get-CippTable -tablename 'TenantGroups'
17+
$Results = [System.Collections.Generic.List[object]]::new()
18+
$ExistingGroups = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'TenantGroup' and Type eq 'dynamic'"
19+
$DefaultGroups =
20+
21+
foreach ($Group in $DefaultGroups) {
22+
# Check if group with same name already exists
23+
$ExistingGroup = $ExistingGroups | Where-Object -Property Name -EQ $group.Name
24+
if ($ExistingGroup) {
25+
$Results.Add(@{
26+
resultText = "Group '$($Group.Name)' already exists, skipping"
27+
state = 'warning'
28+
})
29+
continue
30+
}
31+
$GroupEntity = @{
32+
PartitionKey = 'TenantGroup'
33+
RowKey = $groupId
34+
Name = $Group.Name
35+
Description = $Group.Description
36+
GroupType = $Group.GroupType
37+
DynamicRules = $Group.DynamicRules
38+
RuleLogic = $Group.RuleLogic
39+
}
40+
Add-CIPPAzDataTableEntity @Table -Entity $GroupEntity -Force
41+
42+
$Results.Add(@{
43+
resultText = "Created default group: '$($Group.Name)'"
44+
state = 'success'
45+
})
46+
47+
Write-LogMessage -API 'TenantGroups' -message "Created default tenant group: $($Group.Name)" -sev Info
48+
}
49+
50+
$Body = @{ Results = $Results }
51+
52+
return ([HttpResponseContext]@{
53+
StatusCode = [HttpStatusCode]::OK
54+
Body = $Body
55+
})
56+
} catch {
57+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
58+
Write-LogMessage -API 'TenantGroups' -message "Failed to create default groups: $ErrorMessage" -sev Error
59+
$Body = @{ Results = "Failed to create default groups: $ErrorMessage" }
60+
return ([HttpResponseContext]@{
61+
StatusCode = [HttpStatusCode]::InternalServerError
62+
Body = $Body
63+
})
64+
}
65+
}

Modules/CIPPCore/Public/TenantGroups/Update-CIPPDynamicTenantGroups.ps1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,28 @@ function Update-CIPPDynamicTenantGroups {
5252
if ($Operator -in @('in', 'notin')) {
5353
$arrayValues = if ($Value -is [array]) { $Value.guid } else { @($Value.guid) }
5454
$arrayAsString = $arrayValues | ForEach-Object { "'$_'" }
55-
"(`$_.skuId | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0"
55+
if ($Operator -eq 'in') {
56+
"(`$_.skuId | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0"
57+
} else {
58+
"(`$_.skuId | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -eq 0"
59+
}
5660
} else {
57-
"`$_.skuId -contains '$($Value.guid)'"
61+
"`$_.skuId -$Operator '$($Value.guid)'"
5862
}
5963
}
6064
'availableServicePlan' {
6165
if ($Operator -in @('in', 'notin')) {
6266
$arrayValues = if ($Value -is [array]) { $Value.value } else { @($Value.value) }
6367
$arrayAsString = $arrayValues | ForEach-Object { "'$_'" }
64-
"(`$_.servicePlans | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0"
68+
if ($Operator -eq 'in') {
69+
# Keep tenants with ANY of the provided plans
70+
"(`$_.servicePlans | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0"
71+
} else {
72+
# Exclude tenants with ANY of the provided plans
73+
"(`$_.servicePlans | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -eq 0"
74+
}
6575
} else {
66-
"`$_.servicePlans -contains '$($Value.value)'"
76+
"`$_.servicePlans -$Operator '$($Value.value)'"
6777
}
6878
}
6979
default {
@@ -73,6 +83,9 @@ function Update-CIPPDynamicTenantGroups {
7383
}
7484

7585
}
86+
if (!$WhereConditions) {
87+
throw 'Generating the conditions failed. The conditions seem to be empty.'
88+
}
7689
$TenantObj = $AllTenants | ForEach-Object {
7790
if ($Rules.property -contains 'availableLicense') {
7891
$LicenseInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/subscribedSkus' -TenantId $_.defaultDomainName

0 commit comments

Comments
 (0)