Functions/Get-CredentialFromMSPCompleteEndpoint.ps1

<#
.SYNOPSIS
    This function retrieves the credential (username + password) from a MSPComplete Endpoint.
.DESCRIPTION
    This function retrieves the credential (username + password) from a MSPComplete Endpoint.
    There is no pre-processing required on the Endpoint, whether it is passed in using
    the MSPComplete platform or retrieved using Get-BT_Endpoint.
#>

function Get-CredentialFromMSPCompleteEndpoint {
    param (
        # The ID of the MSPComplete Endpoint
        [Parameter(Mandatory=$true, ParameterSetName="endpointID")]
        [String]$endpointID,

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

        # The MSPComplete Ticket
        [Parameter(Mandatory=$false)]
        $ticket = $mspc.Ticket,

        # The environment where the endpoint is stored.
        [Parameter(Mandatory=$false)]
        [ValidateSet("BT", "Beta", "Develop", "Release")]
        [String]$environment = "Beta"
    )

    # Endpoint is provided and it already has a "Credential" property
    # This case applies when the endpoint is passed in on the MSPComplete platform
    if ($null -ne $endpoint.Credential -and ![String]::IsNullOrWhiteSpace($endpoint.Credential.UserName) -and $null -ne $endpoint.Credential.Password) {
        Write-Information "Using the 'Credential' property from the Endpoint '$($endpoint.Name)'."
        return $endpoint.Credential
    }

    # Retrieve the endpoint ID from the endpoint
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        $endpointID = $endpoint.Id
    }

    # Retrieve the endpoint with a credential property, and return the credential
    Write-Information "Retrieving the Endpoint with a 'Credential' property."
    return (Get-MSPCompleteEndpointWithCredential -EndpointID $endpointID -Ticket $ticket -Environment $environment).Credential
}