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 if ($NameServer.NameHost) { $NameServer = $NameServer.NameHost } else { $NameServer = 'Not Found'} $DNSRecords = Get-CuraDNSRecords -DomainName $DomainName if($NameServer -like '*.curanet.dk' -and $DNSRecords.status -ne 404) { $NewRecord = New-CuraDNSRecord -DomainName $DomainName -Type "TXT" -Value $VerificationCode if($NewRecord.status -ne 404) { Write-Host "Successfully created DNS record on Curanet" -ForegroundColor Green } else { Write-Host "Failed to create DNS record on Curanet" -ForegroundColor Red } } 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 Confirm-MgDomain -DomainId $DomainName | Out-Null 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: $_" } } |