Skip to content

Commit 2f6e6ea

Browse files
add single tenants improvements
1 parent c8c04b3 commit 2f6e6ea

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Setup/Invoke-ExecAddTenant.ps1

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Function Invoke-ExecAddTenant {
1313
try {
1414
# Get the tenant ID from the request body
1515
$tenantId = $Request.body.tenantId
16-
$displayName = $Request.body.displayName
1716
$defaultDomainName = $Request.body.defaultDomainName
1817

1918
# Get the Tenants table
2019
$TenantsTable = Get-CippTable -tablename 'Tenants'
21-
20+
#force a refresh of the authentication info
21+
$auth = Get-CIPPAuthentication
2222
# Check if tenant already exists
2323
$ExistingTenant = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and RowKey eq '$tenantId'"
2424

@@ -30,25 +30,13 @@ Function Invoke-ExecAddTenant {
3030
} else {
3131
# Create new tenant entry
3232
try {
33-
# Get organization info
34-
$Organization = New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/organization' -tenantid $tenantId -NoAuthCheck:$true -ErrorAction Stop
35-
36-
if (-not $displayName) {
37-
$displayName = $Organization[0].displayName
38-
}
39-
40-
if (-not $defaultDomainName) {
41-
# Try to get domains
42-
try {
43-
$Domains = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/domains?$top=999' -tenantid $tenantId -NoAuthCheck:$true -ErrorAction Stop
44-
$defaultDomainName = ($Domains | Where-Object { $_.isDefault -eq $true }).id
45-
$initialDomainName = ($Domains | Where-Object { $_.isInitial -eq $true }).id
46-
} catch {
47-
# If we can't get domains, use verified domains from organization
48-
$defaultDomainName = ($Organization[0].verifiedDomains | Where-Object { $_.isDefault -eq $true }).name
49-
$initialDomainName = ($Organization[0].verifiedDomains | Where-Object { $_.isInitial -eq $true }).name
50-
}
51-
}
33+
# Get tenant information from Microsoft Graph
34+
$headers = @{ Authorization = "Bearer $($request.body.access_token)" }
35+
$Organization = (Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/organization' -Headers $headers -Method GET -ContentType 'application/json' -ErrorAction Stop).value
36+
$displayName = $Organization.displayName
37+
$Domains = (Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/domains?$top=999' -Headers $headers -Method GET -ContentType 'application/json' -ErrorAction Stop).value
38+
$defaultDomainName = ($Domains | Where-Object { $_.isDefault -eq $true }).id
39+
$initialDomainName = ($Domains | Where-Object { $_.isInitial -eq $true }).id
5240
} catch {
5341
Write-LogMessage -API 'Add-Tenant' -message "Failed to get information for tenant $tenantId - $($_.Exception.Message)" -Sev 'Critical'
5442
throw "Failed to get information for tenant $tenantId. Make sure the tenant is properly authenticated."

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function Get-CIPPAuthentication {
2020
}
2121
Write-Host "Got secrets from dev storage. ApplicationID: $env:ApplicationID"
2222
#Get list of tenants that have 'directTenant' set to true
23-
$tenants = Get-Tenants -IncludeErrors | Where-Object -Property delegatedPrivilegeStatus -EQ 'directTenant'
23+
#get directtenants directly from table, avoid get-tenants due to performance issues
24+
$TenantsTable = Get-CippTable -tablename 'Tenants'
25+
$Filter = "PartitionKey eq 'Tenants' and delegatedPrivilegeStatus eq 'directTenant'"
26+
$tenants = Get-CIPPAzDataTableEntity @TenantsTable -Filter $Filter
2427
if ($tenants) {
2528
$tenants | ForEach-Object {
2629
$secretname = $_.customerId -replace '-', '_'
@@ -49,7 +52,9 @@ function Get-CIPPAuthentication {
4952

5053
$keyvaultname = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
5154
#Get list of tenants that have 'directTenant' set to true
52-
$tenants = Get-Tenants -IncludeErrors | Where-Object -Property delegatedPrivilegeStatus -EQ 'directTenant'
55+
$TenantsTable = Get-CippTable -tablename 'Tenants'
56+
$Filter = "PartitionKey eq 'Tenants' and delegatedPrivilegeStatus eq 'directTenant'"
57+
$tenants = Get-CIPPAzDataTableEntity @TenantsTable -Filter $Filter
5358
if ($tenants) {
5459
$tenants | ForEach-Object {
5560
$name = $_.tenantId -replace '-', '_'

Modules/CIPPCore/Public/GraphHelper/Get-GraphToken.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $AppSecret, $refreshT
44
Internal
55
#>
66
if (!$scope) { $scope = 'https://graph.microsoft.com/.default' }
7+
78
if (!$env:SetFromProfile) { $CIPPAuth = Get-CIPPAuthentication; Write-Host 'Could not get Refreshtoken from environment variable. Reloading token.' }
89
#If the $env:<$tenantid> is set, use that instead of the refreshtoken for all tenants.
910
$refreshToken = $env:RefreshToken
1011
if (!$tenantid) { $tenantid = $env:TenantID }
11-
$ClientType = Get-Tenants -IncludeErrors -TenantFilter $tenantid
12+
#Get list of tenants that have 'directTenant' set to true
13+
#get directtenants directly from table, avoid get-tenants due to performance issues
14+
$TenantsTable = Get-CippTable -tablename 'Tenants'
15+
$Filter = "PartitionKey eq 'Tenants' and delegatedPrivilegeStatus eq 'directTenant'"
16+
$ClientType = Get-CIPPAzDataTableEntity @TenantsTable -Filter $Filter | Where-Object { $_.customerId -eq $tenantid -or $_.defaultDomainName -eq $tenantid }
1217
if ($clientType.delegatedPrivilegeStatus -eq 'directTenant') {
1318
Write-Host "Using direct tenant refresh token for $($clientType.customerId)"
1419
$ClientRefreshToken = Get-Item -Path "env:\$($clientType.customerId)" -ErrorAction SilentlyContinue

Modules/CIPPCore/Public/GraphHelper/Get-Tenants.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ function Get-Tenants {
1414
[switch]$CleanOld,
1515
[string]$TenantFilter
1616
)
17-
17+
$caller = $MyInvocation.InvocationName
18+
$scriptName = $MyInvocation.ScriptName
19+
Write-Host "Called by: $caller"
20+
Write-Host "In script: $scriptName"
1821
$TenantsTable = Get-CippTable -tablename 'Tenants'
1922
$ExcludedFilter = "PartitionKey eq 'Tenants' and Excluded eq true"
2023

@@ -75,7 +78,9 @@ function Get-Tenants {
7578
if (($BuildRequired -or $TriggerRefresh.IsPresent) -and $PartnerTenantState.state -ne 'owntenant') {
7679
# Get TenantProperties table
7780
$PropertiesTable = Get-CippTable -TableName 'TenantProperties'
78-
81+
if (!$env:RefreshToken) {
82+
throw 'RefreshToken not set. Cannot get tenant list.'
83+
}
7984
#get the full list of tenants
8085
$GDAPRelationships = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/tenantRelationships/delegatedAdminRelationships?`$filter=status eq 'active' and not startsWith(displayName,'MLT_')$RelationshipFilter&`$select=customer,autoExtendDuration,endDateTime&`$top=300" -NoAuthCheck:$true
8186
$GDAPList = foreach ($Relationship in $GDAPRelationships) {

0 commit comments

Comments
 (0)