|
| 1 | +function Update-CIPPDynamicTenantGroups { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Update dynamic tenant groups based on their rules |
| 5 | + .DESCRIPTION |
| 6 | + This function processes dynamic tenant group rules and updates membership accordingly |
| 7 | + .PARAMETER GroupId |
| 8 | + The specific group ID to update. If not provided, all dynamic groups will be updated |
| 9 | + .FUNCTIONALITY |
| 10 | + Internal |
| 11 | + #> |
| 12 | + [CmdletBinding()] |
| 13 | + param( |
| 14 | + [string]$GroupId |
| 15 | + ) |
| 16 | + |
| 17 | + try { |
| 18 | + $GroupTable = Get-CippTable -tablename 'TenantGroups' |
| 19 | + $MembersTable = Get-CippTable -tablename 'TenantGroupMembers' |
| 20 | + |
| 21 | + if ($GroupId) { |
| 22 | + $DynamicGroups = Get-CIPPAzDataTableEntity @GroupTable -Filter "PartitionKey eq 'TenantGroup' and RowKey eq '$GroupId'" |
| 23 | + } else { |
| 24 | + $DynamicGroups = Get-CIPPAzDataTableEntity @GroupTable -Filter "PartitionKey eq 'TenantGroup' and GroupType eq 'dynamic'" |
| 25 | + } |
| 26 | + |
| 27 | + if (-not $DynamicGroups) { |
| 28 | + Write-LogMessage -API 'TenantGroups' -message 'No dynamic groups found to process' -sev Info |
| 29 | + return @{ MembersAdded = 0; MembersRemoved = 0; GroupsProcessed = 0 } |
| 30 | + } |
| 31 | + |
| 32 | + $AllTenants = Get-Tenants -IncludeErrors |
| 33 | + $TotalMembersAdded = 0 |
| 34 | + $TotalMembersRemoved = 0 |
| 35 | + $GroupsProcessed = 0 |
| 36 | + |
| 37 | + foreach ($Group in $DynamicGroups) { |
| 38 | + try { |
| 39 | + Write-LogMessage -API 'TenantGroups' -message "Processing dynamic group: $($Group.Name)" -sev Info |
| 40 | + $Rules = $Group.DynamicRules | ConvertFrom-Json |
| 41 | + # Build a single Where-Object string for AND logic |
| 42 | + $WhereConditions = foreach ($Rule in $Rules) { |
| 43 | + $Property = $Rule.property |
| 44 | + $Operator = $Rule.operator |
| 45 | + $Value = $Rule.value |
| 46 | + |
| 47 | + switch ($Property) { |
| 48 | + 'delegatedAccessStatus' { |
| 49 | + "`$_.delegatedPrivilegeStatus -$Operator '$($Value.value)'" |
| 50 | + } |
| 51 | + 'availableLicense' { |
| 52 | + if ($Operator -in @('in', 'notin')) { |
| 53 | + $arrayValues = if ($Value -is [array]) { $Value.guid } else { @($Value.guid) } |
| 54 | + $arrayAsString = $arrayValues | ForEach-Object { "'$_'" } |
| 55 | + "(`$_.skuId | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0" |
| 56 | + } else { |
| 57 | + "`$_.skuId -contains '$($Value.guid)'" |
| 58 | + } |
| 59 | + } |
| 60 | + 'availableServicePlan' { |
| 61 | + if ($Operator -in @('in', 'notin')) { |
| 62 | + $arrayValues = if ($Value -is [array]) { $Value.value } else { @($Value.value) } |
| 63 | + $arrayAsString = $arrayValues | ForEach-Object { "'$_'" } |
| 64 | + "(`$_.servicePlans | Where-Object { `$_ -in @($($arrayAsString -join ', ')) }).Count -gt 0" |
| 65 | + } else { |
| 66 | + "`$_.servicePlans -contains '$($Value.value)'" |
| 67 | + } |
| 68 | + } |
| 69 | + default { |
| 70 | + Write-LogMessage -API 'TenantGroups' -message "Unknown property type: $Property" -sev Warning |
| 71 | + $null |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + $TenantObj = $AllTenants | ForEach-Object { |
| 77 | + $LicenseInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/subscribedSkus' -TenantId $_.defaultDomainName |
| 78 | + $SKUId = $LicenseInfo.SKUId |
| 79 | + $ServicePlans = (Get-CIPPTenantCapabilities -TenantFilter $_.defaultDomainName).psobject.properties.name |
| 80 | + [pscustomobject]@{ |
| 81 | + customerId = $_.customerId |
| 82 | + defaultDomainName = $_.defaultDomainName |
| 83 | + displayName = $_.displayName |
| 84 | + skuId = $SKUId |
| 85 | + servicePlans = $ServicePlans |
| 86 | + delegatedPrivilegeStatus = $_.delegatedPrivilegeStatus |
| 87 | + } |
| 88 | + } |
| 89 | + # Combine all conditions with the specified logic (AND or OR) |
| 90 | + $LogicOperator = if ($Group.RuleLogic -eq 'or') { ' -or ' } else { ' -and ' } |
| 91 | + $WhereString = $WhereConditions -join $LogicOperator |
| 92 | + $ScriptBlock = [ScriptBlock]::Create($WhereString) |
| 93 | + $MatchingTenants = $TenantObj | Where-Object $ScriptBlock |
| 94 | + |
| 95 | + $CurrentMembers = Get-CIPPAzDataTableEntity @MembersTable -Filter "PartitionKey eq 'Member' and GroupId eq '$($Group.RowKey)'" |
| 96 | + $CurrentMemberIds = $CurrentMembers.customerId |
| 97 | + $NewMemberIds = $MatchingTenants.customerId |
| 98 | + |
| 99 | + $ToAdd = $NewMemberIds | Where-Object { $_ -notin $CurrentMemberIds } |
| 100 | + $ToRemove = $CurrentMemberIds | Where-Object { $_ -notin $NewMemberIds } |
| 101 | + |
| 102 | + foreach ($TenantId in $ToAdd) { |
| 103 | + $TenantInfo = $AllTenants | Where-Object { $_.customerId -eq $TenantId } |
| 104 | + $MemberEntity = @{ |
| 105 | + PartitionKey = 'Member' |
| 106 | + RowKey = '{0}-{1}' -f $Group.RowKey, $TenantId |
| 107 | + GroupId = $Group.RowKey |
| 108 | + customerId = "$TenantId" |
| 109 | + } |
| 110 | + Add-CIPPAzDataTableEntity @MembersTable -Entity $MemberEntity -Force |
| 111 | + Write-LogMessage -API 'TenantGroups' -message "Added tenant '$($TenantInfo.displayName)' to dynamic group '$($Group.Name)'" -sev Info |
| 112 | + $TotalMembersAdded++ |
| 113 | + } |
| 114 | + |
| 115 | + foreach ($TenantId in $ToRemove) { |
| 116 | + $TenantInfo = $AllTenants | Where-Object { $_.customerId -eq $TenantId } |
| 117 | + $MemberToRemove = $CurrentMembers | Where-Object { $_.customerId -eq $TenantId } |
| 118 | + if ($MemberToRemove) { |
| 119 | + Remove-AzDataTableEntity @MembersTable -Entity $MemberToRemove -Force |
| 120 | + Write-LogMessage -API 'TenantGroups' -message "Removed tenant '$($TenantInfo.displayName)' from dynamic group '$($Group.Name)'" -sev Info |
| 121 | + $TotalMembersRemoved++ |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + $GroupsProcessed++ |
| 126 | + Write-LogMessage -API 'TenantGroups' -message "Group '$($Group.Name)' updated: +$($ToAdd.Count) members, -$($ToRemove.Count) members" -sev Info |
| 127 | + |
| 128 | + } catch { |
| 129 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 130 | + Write-LogMessage -API 'TenantGroups' -message "Failed to process group '$($Group.Name)': $ErrorMessage" -sev Error |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Write-LogMessage -API 'TenantGroups' -message "Dynamic tenant group update completed. Groups processed: $GroupsProcessed, Members added: $TotalMembersAdded, Members removed: $TotalMembersRemoved" -sev Info |
| 135 | + |
| 136 | + return @{ |
| 137 | + MembersAdded = $TotalMembersAdded |
| 138 | + MembersRemoved = $TotalMembersRemoved |
| 139 | + GroupsProcessed = $GroupsProcessed |
| 140 | + } |
| 141 | + |
| 142 | + } catch { |
| 143 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 144 | + Write-LogMessage -API 'TenantGroups' -message "Failed to update dynamic tenant groups: $ErrorMessage" -sev Error |
| 145 | + throw |
| 146 | + } |
| 147 | +} |
| 148 | + |
0 commit comments