Skip to content

Commit

Permalink
Merge branch 'KelvinTegelaar:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldeteves authored Apr 16, 2024
2 parents 4e1a903 + 5e7242a commit 6292432
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
47 changes: 31 additions & 16 deletions Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function Get-Tenants {
[switch]$IncludeAll,
[switch]$IncludeErrors,
[switch]$SkipDomains,
[switch]$TriggerRefresh
[switch]$TriggerRefresh,
[switch]$CleanOld
)

$TenantsTable = Get-CippTable -tablename 'Tenants'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'

}
}

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion version_latest.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.5.2
5.5.3

0 comments on commit 6292432

Please sign in to comment.