Functions/Connect-MicrosoftTeamsAdminAccount.ps1

<#
.SYNOPSIS
    This function connects to Microsoft Teams using admin account credentials or a MSPComplete Endpoint.
.DESCRIPTION
    This function connects to Microsoft Teams using admin account credentials or a MSPComplete Endpoint.
    It returns whether the connection and logon was successful.
.EXAMPLE
    Connect-MicrosoftTeamsAdminAccount -Endpoint $Endpoint
.EXAMPLE
    $Endpoint | Connect-MicrosoftTeamsAdminAccount
.EXAMPLE
    Connect-MicrosoftTeamsAdminAccount -Username $username -Password $password
#>

function Connect-MicrosoftTeamsAdminAccount {
    param (
        # The username of the Microsoft Teams account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [String]$username,

        # The password of the Microsoft Teams account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [SecureString]$password,

        # The MSPComplete Endpoint.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        $endpoint
    )

    # If given endpoint, retrieve credential directly
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        $microsoftTeamsCredential = $endpoint.Credential
        $username = $microsoftTeamsCredential.Username
    }

    # Create the Microsoft Teams credential from the given username and password
    else {
        $microsoftTeamsCredential = [PSCredential]::new($username, $password)
    }

    # Logon to Microsoft Teams
    try {
        Connect-MicrosoftTeams -Credential $microsoftTeamsCredential -ErrorAction Stop | Out-Null

        # Logon was successful
        Write-Information "Connection and logon to Microsoft Teams successful with username '$($username)'."
        return $true
    }

    # Logon was unsuccessful
    catch {
        Write-Error "Failed Microsoft Teams account login with username '$($username)'.`r`n$($_.Exception.Message)"
        return $false
    }
}