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 = '' 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) } 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