@@ -37,7 +37,7 @@ function Update-CIPPDynamicTenantGroups {
3737 foreach ($Group in $DynamicGroups ) {
3838 try {
3939 Write-LogMessage - API ' TenantGroups' - message " Processing dynamic group: $ ( $Group.Name ) " - sev Info
40- $Rules = $Group.DynamicRules | ConvertFrom-Json
40+ $Rules = @ ( $Group.DynamicRules | ConvertFrom-Json )
4141 # Build a single Where-Object string for AND logic
4242 $WhereConditions = foreach ($Rule in $Rules ) {
4343 $Property = $Rule.property
@@ -74,8 +74,10 @@ function Update-CIPPDynamicTenantGroups {
7474
7575 }
7676 $TenantObj = $AllTenants | ForEach-Object {
77- $LicenseInfo = New-GraphGetRequest - uri ' https://graph.microsoft.com/v1.0/subscribedSkus' - TenantId $_.defaultDomainName
78- $SKUId = $LicenseInfo.SKUId
77+ if ($Rules.property -contains ' availableLicense' ) {
78+ $LicenseInfo = New-GraphGetRequest - uri ' https://graph.microsoft.com/v1.0/subscribedSkus' - TenantId $_.defaultDomainName
79+ }
80+ $SKUId = $LicenseInfo.SKUId ?? @ ()
7981 $ServicePlans = (Get-CIPPTenantCapabilities - TenantFilter $_.defaultDomainName ).psobject.properties.name
8082 [pscustomobject ]@ {
8183 customerId = $_.customerId
@@ -89,9 +91,13 @@ function Update-CIPPDynamicTenantGroups {
8991 # Combine all conditions with the specified logic (AND or OR)
9092 $LogicOperator = if ($Group.RuleLogic -eq ' or' ) { ' -or ' } else { ' -and ' }
9193 $WhereString = $WhereConditions -join $LogicOperator
94+ Write-Information " Evaluating tenants with condition: $WhereString "
95+
9296 $ScriptBlock = [ScriptBlock ]::Create($WhereString )
9397 $MatchingTenants = $TenantObj | Where-Object $ScriptBlock
9498
99+ Write-Information " Found $ ( $MatchingTenants.Count ) matching tenants for group '$ ( $Group.Name ) '"
100+
95101 $CurrentMembers = Get-CIPPAzDataTableEntity @MembersTable - Filter " PartitionKey eq 'Member' and GroupId eq '$ ( $Group.RowKey ) '"
96102 $CurrentMemberIds = $CurrentMembers.customerId
97103 $NewMemberIds = $MatchingTenants.customerId
0 commit comments