@@ -13,12 +13,12 @@ Function Invoke-ExecAddTenant {
13
13
try {
14
14
# Get the tenant ID from the request body
15
15
$tenantId = $Request.body.tenantId
16
- $displayName = $Request.body.displayName
17
16
$defaultDomainName = $Request.body.defaultDomainName
18
17
19
18
# Get the Tenants table
20
19
$TenantsTable = Get-CippTable - tablename ' Tenants'
21
-
20
+ # force a refresh of the authentication info
21
+ $auth = Get-CIPPAuthentication
22
22
# Check if tenant already exists
23
23
$ExistingTenant = Get-CIPPAzDataTableEntity @TenantsTable - Filter " PartitionKey eq 'Tenants' and RowKey eq '$tenantId '"
24
24
@@ -30,25 +30,13 @@ Function Invoke-ExecAddTenant {
30
30
} else {
31
31
# Create new tenant entry
32
32
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
52
40
} catch {
53
41
Write-LogMessage - API ' Add-Tenant' - message " Failed to get information for tenant $tenantId - $ ( $_.Exception.Message ) " - Sev ' Critical'
54
42
throw " Failed to get information for tenant $tenantId . Make sure the tenant is properly authenticated."
0 commit comments