@@ -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."
0 commit comments