From f15a4bd4e2cade0fd0f3413760bed0b55c2a2e84 Mon Sep 17 00:00:00 2001 From: Woody Date: Mon, 15 Apr 2024 21:27:31 -0400 Subject: [PATCH 1/3] Add Tenant ID to output object in Invoke-ListTenantDetails --- .../Tenant/Administration/Tenant/Invoke-ListTenantDetails.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-ListTenantDetails.ps1 b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-ListTenantDetails.ps1 index 85e3330926c0..9aa191e2e44f 100644 --- a/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-ListTenantDetails.ps1 +++ b/Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Tenant/Invoke-ListTenantDetails.ps1 @@ -14,13 +14,14 @@ Function Invoke-ListTenantDetails { try { $tenantfilter = $Request.Query.TenantFilter - $org = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/organization' -tenantid $tenantfilter | Select-Object displayName, city, country, countryLetterCode, street, state, postalCode, + $org = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/organization' -tenantid $tenantfilter | Select-Object displayName, id, city, country, countryLetterCode, street, state, postalCode, @{ Name = 'businessPhones'; Expression = { $_.businessPhones -join ', ' } }, @{ Name = 'technicalNotificationMails'; Expression = { $_.technicalNotificationMails -join ', ' } }, tenantType, createdDateTime, onPremisesLastPasswordSyncDateTime, onPremisesLastSyncDateTime, onPremisesSyncEnabled, assignedPlans } catch { $org = [PSCustomObject]@{ displayName = 'Error loading tenant' + id = '' city = '' country = '' countryLetterCode = '' From 9fb0d67a5c4fe2ceaecf47dc2ac1d5b946b25a19 Mon Sep 17 00:00:00 2001 From: KelvinTegelaar Date: Tue, 16 Apr 2024 10:37:42 +0200 Subject: [PATCH 2/3] Fixed Tenant retrieval and caching --- .../Public/GraphHelper/Get-Tenants.ps1 | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1 b/Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1 index 2cb51e4d929a..02196d6f58e4 100644 --- a/Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1 +++ b/Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1 @@ -10,7 +10,8 @@ function Get-Tenants { [switch]$IncludeAll, [switch]$IncludeErrors, [switch]$SkipDomains, - [switch]$TriggerRefresh + [switch]$TriggerRefresh, + [switch]$CleanOld ) $TenantsTable = Get-CippTable -tablename 'Tenants' @@ -34,6 +35,22 @@ function Get-Tenants { $BuildRequired = $true } + if ($CleanOld) { + $GDAPRelationships = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/tenantRelationships/delegatedAdminRelationships?`$filter=status eq 'active' and not startsWith(displayName,'MLT_')&`$select=customer,autoExtendDuration,endDateTime" -NoAuthCheck:$true + $GDAPList = foreach ($Relationship in $GDAPRelationships) { + [PSCustomObject]@{ + customerId = $Relationship.customer.tenantId + displayName = $Relationship.customer.displayName + autoExtend = ($Relationship.autoExtendDuration -ne 'PT0S') + relationshipEnd = $Relationship.endDateTime + } + } + $CurrentTenants = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and Excluded eq false" + $CurrentTenants | Where-Object { $_.customerId -notin $GDAPList.customerId } | ForEach-Object { + Remove-AzDataTableEntity @TenantsTable -Entity $_ + } + } + if ($BuildRequired -or $TriggerRefresh.IsPresent) { #get the full list of tenants $GDAPRelationships = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/tenantRelationships/delegatedAdminRelationships?`$filter=status eq 'active' and not startsWith(displayName,'MLT_')&`$select=customer,autoExtendDuration,endDateTime" -NoAuthCheck:$true @@ -45,16 +62,15 @@ function Get-Tenants { relationshipEnd = $Relationship.endDateTime } } + $ActiveRelationships = $GDAPList | Where-Object { $_.customerId -notin $SkipListCache.customerId } - $TenantList = $ActiveRelationships | Group-Object -Property customerId | ForEach-Object -Parallel { + $TenantList = $ActiveRelationships | Group-Object -Property customerId | ForEach-Object { Write-Host "Processing $($_.Name) to add to tenant list." - Import-Module CIPPCore - Import-Module AzBobbyTables - $ExistingTenantInfo = Get-CIPPAzDataTableEntity @using:TenantsTable -Filter "PartitionKey eq 'Tenants' and RowKey eq '$($_.Name)'" - if ($ExistingTenantInfo -and $ExistingInfo.RequiresRefresh -eq $false) { + $ExistingTenantInfo = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and RowKey eq '$($_.Name)'" + if ($ExistingTenantInfo -and $ExistingTenantInfo.RequiresRefresh -eq $false) { Write-Host 'Existing tenant found. We already have it cached, skipping.' $ExistingTenantInfo - continue + return } $LatestRelationship = $_.Group | Sort-Object -Property relationshipEnd | Select-Object -Last 1 $AutoExtend = ($_.Group | Where-Object { $_.autoExtend -eq $true } | Measure-Object).Count -gt 0 @@ -75,7 +91,6 @@ function Get-Tenants { } catch { Write-LogMessage -API 'Get-Tenants' -message "Tried adding $($LatestRelationship.customerId) to tenant list but failed to get domains - $($_.Exception.Message)" -level 'Critical' - } } @@ -120,17 +135,17 @@ function Get-Tenants { }) | Out-Null } foreach ($Tenant in $TenantList) { - if ($Tenant.defaultDomainName -eq 'Invalid' -or !$Tenant.defaultDomainName) { continue } + if ($Tenant.defaultDomainName -eq 'Invalid' -or !$Tenant.defaultDomainName) { + Write-LogMessage -API 'Get-Tenants' -message "We're skipping $($Tenant.displayName) as it has an invalid default domain name. Something is up with this instance." -level 'Critical' + continue + } $IncludedTenantsCache.Add($Tenant) | Out-Null } - } - - if ($IncludedTenantsCache) { - Add-CIPPAzDataTableEntity @TenantsTable -Entity $IncludedTenantsCache -Force - $CurrentTenants = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and Excluded eq false" - $CurrentTenants | Where-Object { $_.customerId -notin $IncludedTenantsCache.customerId } | ForEach-Object { - Remove-AzDataTableEntity @TenantsTable -Entity $_ + if ($IncludedTenantsCache) { + Add-CIPPAzDataTableEntity @TenantsTable -Entity $IncludedTenantsCache -Force | Out-Null } } + + return ($IncludedTenantsCache | Where-Object { $null -ne $_.defaultDomainName -and ($_.defaultDomainName -notmatch 'Domain Error' -or $IncludeAll.IsPresent) } | Sort-Object -Property displayName) } From 7623f9c65aa0b79532f4bcd796c0f7a4fc14daf8 Mon Sep 17 00:00:00 2001 From: KelvinTegelaar Date: Tue, 16 Apr 2024 10:39:11 +0200 Subject: [PATCH 3/3] Tenant cache --- version_latest.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version_latest.txt b/version_latest.txt index 9af9a6a81c7e..d2ff458a0121 100644 --- a/version_latest.txt +++ b/version_latest.txt @@ -1 +1 @@ -5.5.2 \ No newline at end of file +5.5.3 \ No newline at end of file