Skip to content

Commit c09229f

Browse files
authored
Merge pull request #511 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 68157ec + 78d2927 commit c09229f

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 = '[{"PartitionKey":"TenantGroup","RowKey":"369d985e-0fba-48f9-844f-9f793b10a12c","Description":"This group does not have a license for intune, nor a license for Entra ID Premium","Description@type":null,"DynamicRules":"[{\"property\":\"availableServicePlan\",\"operator\":\"notIn\",\"value\":[{\"label\":\"Microsoft Intune\",\"value\":\"INTUNE_A\",\"id\":\"c1ec4a95-1f05-45b3-a911-aa3fa01094f5\"}]},{\"property\":\"availableServicePlan\",\"operator\":\"notIn\",\"value\":[{\"label\":\"Microsoft Entra ID P1\",\"value\":\"AAD_PREMIUM\",\"id\":\"41781fb2-bc02-4b7c-bd55-b576c07bb09d\"}]}]","DynamicRules@type":null,"GroupType":"dynamic","GroupType@type":null,"RuleLogic":"and","RuleLogic@type":null,"Name":"Not Intune and Entra Premium Capable","Name@type":null},{"PartitionKey":"TenantGroup","RowKey":"4dbca08b-7dc5-4e0f-bc25-14a90c8e0941","Description":"This group has atleast one Business Premium License available","Description@type":null,"DynamicRules":"[{\"property\":\"availableLicense\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft 365 Business Premium\",\"value\":\"SPB\"}]},{\"property\":\"availableLicense\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft 365 Business Premium (no Teams)\",\"value\":\"Microsoft_365_ Business_ Premium_(no Teams)\"}]},{\"property\":\"availableLicense\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft 365 Business Premium Donation\",\"value\":\"Microsoft_365_Business_Premium_Donation_(Non_Profit_Pricing)\"}]},{\"property\":\"availableLicense\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft 365 Business Premium EEA (no Teams)\",\"value\":\"Office_365_w\/o_Teams_Bundle_Business_Premium\"}]}]","DynamicRules@type":null,"GroupType":"dynamic","GroupType@type":null,"RuleLogic":"or","RuleLogic@type":null,"Name":"Business Premium License available","Name@type":null},{"PartitionKey":"TenantGroup","RowKey":"703c0e69-84a8-4dcf-a1c2-4986d2ccc850","Description":"This group does have a license for Entra Premium but does not have a license for Intune","Description@type":null,"DynamicRules":"[{\"property\":\"availableServicePlan\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft Entra ID P1\",\"value\":\"AAD_PREMIUM\",\"id\":\"41781fb2-bc02-4b7c-bd55-b576c07bb09d\"}]},{\"property\":\"availableServicePlan\",\"operator\":\"notIn\",\"value\":[{\"label\":\"Microsoft Intune\",\"value\":\"INTUNE_A\",\"id\":\"c1ec4a95-1f05-45b3-a911-aa3fa01094f5\"}]}]","DynamicRules@type":null,"GroupType":"dynamic","GroupType@type":null,"RuleLogic":"and","RuleLogic@type":null,"Name":"Entra Premium Capable, Not Intune Capable","Name@type":null},{"PartitionKey":"TenantGroup","RowKey":"c1dadbc0-f0b4-448c-a2e6-e1938ba102e0","Description":"This group has Intune and Entra ID Premium available","Description@type":null,"DynamicRules":"{\"property\":\"availableServicePlan\",\"operator\":\"in\",\"value\":[{\"label\":\"Microsoft Intune\",\"value\":\"INTUNE_A\"},{\"label\":\"Microsoft Entra ID P1\",\"value\":\"AAD_PREMIUM\"}]}","DynamicRules@type":null,"GroupType":"dynamic","GroupType@type":null,"RuleLogic":"and","RuleLogic@type":null,"Name":"Entra ID Premium and Intune Capable","Name@type":null}]' | ConvertFrom-Json
20+
21+
22+
foreach ($Group in $DefaultGroups) {
23+
# Check if group with same name already exists
24+
$ExistingGroup = $ExistingGroups | Where-Object -Property Name -EQ $group.Name
25+
if ($ExistingGroup) {
26+
$Results.Add(@{
27+
resultText = "Group '$($Group.Name)' already exists, skipping"
28+
state = 'warning'
29+
})
30+
continue
31+
}
32+
$GroupEntity = @{
33+
PartitionKey = 'TenantGroup'
34+
RowKey = $group.RowKey
35+
Name = $Group.Name
36+
Description = $Group.Description
37+
GroupType = $Group.GroupType
38+
DynamicRules = $Group.DynamicRules
39+
RuleLogic = $Group.RuleLogic
40+
}
41+
Add-CIPPAzDataTableEntity @Table -Entity $GroupEntity -Force
42+
43+
$Results.Add(@{
44+
resultText = "Created default group: '$($Group.Name)'"
45+
state = 'success'
46+
})
47+
48+
Write-LogMessage -API 'TenantGroups' -message "Created default tenant group: $($Group.Name)" -sev Info
49+
}
50+
51+
$Body = @{ Results = $Results }
52+
53+
return ([HttpResponseContext]@{
54+
StatusCode = [HttpStatusCode]::OK
55+
Body = $Body
56+
})
57+
} catch {
58+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
59+
Write-LogMessage -API 'TenantGroups' -message "Failed to create default groups: $ErrorMessage" -sev Error
60+
$Body = @{ Results = "Failed to create default groups: $ErrorMessage" }
61+
return ([HttpResponseContext]@{
62+
StatusCode = [HttpStatusCode]::InternalServerError
63+
Body = $Body
64+
})
65+
}
66+
}

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)