Public/Configuration/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-ModuleLog -Message "Adding domain $($DomainName).." -Level Info -Component 'AddCustomerDomain'

            $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-CuranetDNSRecords -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-CuranetDNSRecord -DomainName $DomainName -Type "TXT" -TTL $TTL -Value $VerificationCode

                if(!$NewRecord.status) {
                    Write-ModuleLog "Successfully created DNS record on Curanet" -Level Info -Component 'AddCustomerDomain'
                }

                else {
                    Write-ModuleLog "Failed to create DNS record on Curanet - $($NewRecord.status)" -Level Error -Component 'AddCustomerDomain'
                    Write-ModuleLog "Please add the following DNS record to your DNS provider:" -Level Warning -Component 'AddCustomerDomain'
                    Write-ModuleLog "Type: TXT" -Level Warning -Component 'AddCustomerDomain'
                    Write-ModuleLog "Name: @ or leave blank" -Level Warning -Component 'AddCustomerDomain'
                    Write-ModuleLog "Value: $($VerificationCode)" -Level Warning -Component 'AddCustomerDomain'
                    Write-ModuleLog "TTL: $($TTL)" -Level Warning -Component 'AddCustomerDomain'
                    Read-Host "Press any key to continue.."
                }
            }
            else {
                Write-ModuleLog "Please add the following DNS record to your DNS provider:" -Level Warning -Component 'AddCustomerDomain'
                Write-ModuleLog "Type: TXT" -Level Warning -Component 'AddCustomerDomain'
                Write-ModuleLog "Name: @ or leave blank" -Level Warning -Component 'AddCustomerDomain'
                Write-ModuleLog "Value: $($VerificationCode)" -Level Warning -Component 'AddCustomerDomain'
                Write-ModuleLog "TTL: 3600" -Level Warning -Component 'AddCustomerDomain'
                Read-Host "Press any key to continue.."
            }

            Write-ModuleLog "Verifying domain.." -Level Info -Component 'AddCustomerDomain'

            Start-Sleep -Seconds 10

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

            while (!$ConfirmDomain) {
                Write-ModuleLog -Message "Domain not verified yet, waiting 10 seconds.." -Level Warning -Component 'AddCustomerDomain'
                Start-Sleep -Seconds 10
                $ConfirmDomain = Confirm-MgDomain -DomainId $DomainName -ErrorAction SilentlyContinue
            }

            if( $IsDefault ) {
                Write-ModuleLog -Message "Setting domain as default.." -Level Info -Component 'AddCustomerDomain'
                Start-Sleep -Seconds 5
                Update-MgDomain -DomainId $DomainName -IsDefault | Out-Null
            }

            Write-ModuleLog -Message "Domain $($DomainName) has been successfully added!" -Level Info -Component 'AddCustomerDomain'
        }
        else {
            Write-ModuleLog -Message "Domain $($DomainName) already exists, skipping.." -Level Warning -Component 'AddCustomerDomain'
        }
    } catch {
        Write-ModuleLog -Message "Failed to add domain" -Level Error -Component 'AddCustomerDomain' -ErrorRecord $_ -ThrowError
    }
}