Functions/Connect-SkypeForBusinessOnlineAdminAccount.ps1

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

function Connect-SkypeForBusinessOnlineAdminAccount {
    param (
        # The username of the Skype for Business Online admin account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [string]$username,

        # The password of the Skype for Business Online admin account.
        [Parameter(Mandatory=$true, ParameterSetName="credential")]
        [SecureString]$password,

        # The MSPComplete Endpoint for the Skype for Business Online admin credentials.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        $endpoint
    )

    # If given endpoint, retrieve username and password
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        $skypeCredential = $endpoint.Credential
        $username = $skypeCredential.Username
    }

    # Create the Skype for Business Online credential from the given username and password
    else {
        $skypeCredential = [PSCredential]::new($username, $password)
    }

    # Logon to Skype for Business Online
    while ($true) {
        try {
            $skypeSession = New-CSOnlineSession -Credential $skypeCredential -ErrorAction Stop
            $skypeSession.Name = "SkypeForBusinessOnline"

            # Additional Import-Module ensures that imported cmdlets are visible globally
            Import-Module (Import-PSSession -Session $skypeSession -DisableNameChecking -AllowClobber -ErrorAction Stop) -Global -ErrorAction Stop
        }
        catch {
            Write-Error "Exception occurred during logon to Skype for Business Online with username '$($username)'.`r`n$($_.Exception.Message)"
            return $false
        }

        # Logon was successful
        Write-Information "Logon to Skype for Business Online was successful with username '$($username)'."
        return $true
    }
}