Public/Shared/Start-BitTitanPreperation.ps1

function Start-BitTitanPreperation {
    param (
        [Parameter(Mandatory)]
        [string]$TenantId
    )

    Clear-Host
    Write-Host "Running BitTitan Preperation for $($TenantId)" -ForegroundColor Green
    try {
        Write-host "
##################################################
# Microsoft 365 Bittitan Forberedelse #
##################################################
"

        Connect-CustomerExchange -CustomerTenantId $TenantId
        Connect-CustomerGraph -CustomerTenantId $TenantId

        $AdminAccount = Get-MgUser -Filter "startswith(UserPrincipalName,'jyskit-adm@')" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty UserPrincipalName

        if (!$AdminAccount) {
            $AdminAccount = Read-Host "Please enter the admin account to use:"
        }

        Write-Host "Adding mailbox permissions.." -ForegroundColor Yellow
        Get-Mailbox -ResultSize Unlimited | Add-MailboxPermission -AccessRights FullAccess -AutoMapping $false -User $AdminAccount

        Write-Host "Setting max send/recieve size.." -ForegroundColor Yellow
        Get-MailboxPlan | Set-MailboxPlan -MaxSendSize 150MB -MaxReceiveSize 150MB
        Get-Mailbox | Set-Mailbox -MaxReceiveSize 150MB -MaxSendSize 150MB

        Write-Host "Enabling Organization customization.." -ForegroundColor Yellow
        Enable-OrganizationCustomization -ErrorAction SilentlyContinue

        Write-Host "Adding management role: ApplicationImpersonation" -ForegroundColor Yellow
        $ManagementRole = New-ManagementRoleAssignment -Role ApplicationImpersonation -User $AdminAccount -ErrorAction Stop
        if(!$ManagementRole) {
            throw "Failed to add management role: ApplicationImpersonation"
        }

        Write-Host "Disabling Focused Inbox.." -ForegroundColor Yellow
        Set-OrganizationConfig -FocusedInboxOn $false
        Get-Mailbox -ResultSize Unlimited | Set-FocusedInbox -FocusedInboxOn $false

        Write-Host "Disabling TNEF.."
        Get-RemoteDomain | Set-RemoteDomain -TNEFEnabled:$false

    }
    catch {
        throw "Failed to run BitTitan Preperation $($TenantId): $_"
    }
}