Public/TenantConfiguration/Add-CustomerDomain.ps1

function Add-CustomerDomain() {
    param(
        [Parameter(Mandatory)]
        [string]$TenantId,

        [Parameter(Mandatory)]
        [string]$DomainName
        )
    
    try {
        $IsDefault = $true

        Connect-CustomerGraph -CustomerTenantId $TenantId

        $Domain = Get-MgDomain -DomainId $DomainName -ErrorAction SilentlyContinue

        $NumberOfDomains = (Get-MgDomain).Count

        if($NumberOfDomains -ne 1) { $IsDefault = $false }

        if($null -eq $Domain) {
            Write-Host "Adding domain $($DomainName).." -ForegroundColor Yellow

            $Params = @{
                Id = $DomainName
            }

            New-MgDomain -BodyParameter $Params | Out-Null

            $VerificationCode = (Get-MgDomainVerificationDnsRecord -DomainId $DomainName | Where-Object { $_.RecordType -eq "TXT" }).AdditionalProperties.text

            $NameServer = Resolve-DnsName -Name $DomainName -Type NS -ErrorAction SilentlyContinue | Select-Object -First 1
            if ($NameServer.NameHost) { $NameServer = $NameServer.NameHost }
            else { $NameServer = 'Not Found'}

            $DNSRecords = Get-CuraDNSRecords -DomainName $DomainName

            if($NameServer -like '*.curanet.dk' -and $DNSRecords.status -notlike "4*") {
                $TTL = $DNSRecords | Where-Object { $_.type -eq "TXT" } | Select-Object -First 1 | Select-Object -ExpandProperty ttl

                if ( !$TTL ) { $TTL = 3600 }

                $NewRecord = New-CuraDNSRecord -DomainName $DomainName -Type "TXT" -TTL $TTL -Value $VerificationCode

                if(!$NewRecord.status) {
                    Write-Host "Successfully created DNS record on Curanet" -ForegroundColor Green
                }

                else {
                    Write-Host "Failed to create DNS record on Curanet - $($NewRecord.status)" -ForegroundColor Red
                    Write-host "Please add the following DNS record to your DNS provider:" -ForegroundColor Yellow
                    Write-host "Type: TXT" -ForegroundColor Yellow
                    Write-host "Name: @ or leave blank" -ForegroundColor Yellow
                    Write-host "Value: $($VerificationCode)" -ForegroundColor Yellow
                    Write-host "TTL: $($TTL)" -ForegroundColor Yellow
                    Read-Host "Press any key to continue.."
                }
            }
            else {
                Write-host "Please add the following DNS record to your DNS provider:" -ForegroundColor Yellow
                Write-host "Type: TXT" -ForegroundColor Yellow
                Write-host "Name: @ or leave blank" -ForegroundColor Yellow
                Write-host "Value: $($VerificationCode)" -ForegroundColor Yellow
                Write-host "TTL: 3600" -ForegroundColor Yellow
                Read-Host "Press any key to continue.."
            }

            Write-Host "Verifying domain.." -ForegroundColor Yellow

            Start-Sleep -Seconds 10

            $ConfirmDomain = Confirm-MgDomain -DomainId $DomainName -ErrorAction SilentlyContinue

            while (!$ConfirmDomain) {
                Write-Host "Domain not verified yet, waiting 10 seconds.." -ForegroundColor Yellow
                Start-Sleep -Seconds 10
                $ConfirmDomain = Confirm-MgDomain -DomainId $DomainName -ErrorAction SilentlyContinue
            }

            if( $IsDefault ) {
                Write-Host "Setting domain as default.." -ForegroundColor Yellow
                Start-Sleep -Seconds 5
                Update-MgDomain -DomainId $DomainName -IsDefault | Out-Null
            }

            Write-Host "Domain $($DomainName) has been successfully added!" -ForegroundColor Green
        }
        else {
            Write-Host "Domain $($DomainName) already exists, skipping.." -ForegroundColor Yellow
        }
    } catch {
        throw "Failed to add domain: $_"
    }
}