|
| 1 | +using namespace System.Net |
| 2 | + |
| 3 | +Function Invoke-ExecAddTenant { |
| 4 | + <# |
| 5 | + .FUNCTIONALITY |
| 6 | + Entrypoint,AnyTenant |
| 7 | + .ROLE |
| 8 | + CIPP.AppSettings.ReadWrite. |
| 9 | + #> |
| 10 | + [CmdletBinding()] |
| 11 | + param($Request, $TriggerMetadata) |
| 12 | + |
| 13 | + try { |
| 14 | + # Get the tenant ID from the request body |
| 15 | + $tenantId = $Request.body.tenantId |
| 16 | + $displayName = $Request.body.displayName |
| 17 | + $defaultDomainName = $Request.body.defaultDomainName |
| 18 | + |
| 19 | + # Get the Tenants table |
| 20 | + $TenantsTable = Get-CippTable -tablename 'Tenants' |
| 21 | + |
| 22 | + # Check if tenant already exists |
| 23 | + $ExistingTenant = Get-CIPPAzDataTableEntity @TenantsTable -Filter "PartitionKey eq 'Tenants' and RowKey eq '$tenantId'" |
| 24 | + |
| 25 | + if ($ExistingTenant) { |
| 26 | + # Update existing tenant |
| 27 | + $ExistingTenant.delegatedPrivilegeStatus = 'directTenant' |
| 28 | + Add-CIPPAzDataTableEntity @TenantsTable -Entity $ExistingTenant -Force | Out-Null |
| 29 | + $Results = @{'message' = 'Successfully updated tenant.'; 'severity' = 'success' } |
| 30 | + } else { |
| 31 | + # Create new tenant entry |
| 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 | + } |
| 52 | + } catch { |
| 53 | + Write-LogMessage -API 'Add-Tenant' -message "Failed to get information for tenant $tenantId - $($_.Exception.Message)" -Sev 'Critical' |
| 54 | + throw "Failed to get information for tenant $tenantId. Make sure the tenant is properly authenticated." |
| 55 | + } |
| 56 | + |
| 57 | + # Create new tenant object |
| 58 | + $NewTenant = [PSCustomObject]@{ |
| 59 | + PartitionKey = 'Tenants' |
| 60 | + RowKey = $tenantId |
| 61 | + customerId = $tenantId |
| 62 | + displayName = $displayName |
| 63 | + defaultDomainName = $defaultDomainName |
| 64 | + initialDomainName = $initialDomainName |
| 65 | + delegatedPrivilegeStatus = 'directTenant' |
| 66 | + domains = '' |
| 67 | + Excluded = $false |
| 68 | + ExcludeUser = '' |
| 69 | + ExcludeDate = '' |
| 70 | + GraphErrorCount = 0 |
| 71 | + LastGraphError = '' |
| 72 | + RequiresRefresh = $false |
| 73 | + LastRefresh = (Get-Date).ToUniversalTime() |
| 74 | + } |
| 75 | + |
| 76 | + # Add tenant to table |
| 77 | + Add-CIPPAzDataTableEntity @TenantsTable -Entity $NewTenant -Force | Out-Null |
| 78 | + $Results = @{'message' = "Successfully added tenant $tenantId to the tenant list with directTenant status."; 'severity' = 'success' } |
| 79 | + } |
| 80 | + } catch { |
| 81 | + $Results = @{'message' = "Failed to add tenant: $($_.Exception.Message)"; 'state' = 'error'; 'severity' = 'error' } |
| 82 | + } |
| 83 | + |
| 84 | + # Associate values to output bindings by calling 'Push-OutputBinding'. |
| 85 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 86 | + StatusCode = [HttpStatusCode]::OK |
| 87 | + Body = $Results |
| 88 | + }) |
| 89 | +} |
0 commit comments