Functions/Download-FileFromSharePointOnline.ps1

<#
.SYNOPSIS
    This function downloads a file from SharePoint Online.
.DESCRIPTION
    This function downloads a file from SharePoint Online.
#>

function Download-FileFromSharePointOnline {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param (
        # The destination of the file on the local machine.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$destinationFilePath,

        # The path of the file on SharePoint Online.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$filePath,

        # # The SharePoint Online site url where the file will be downloaded from.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$siteUrl,

        # The MSPComplete Endpoint containing the Microsoft Graph credentials.
        [Parameter(Mandatory=$true, ParameterSetName="endpoint", ValueFromPipeline=$true)]
        [ValidateNotNull()]
        $endpoint,

        # The Microsoft Graph authentication token.
        [Parameter(Mandatory=$true, ParameterSetName="token")]
        [ValidateNotNullOrEmpty()]
        [String]$token,

        # Select the stream where the messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error")]
        [String]$outputStream = "Error"
    )

    # Retrieve the Microsoft Graph authentication token
    if ($PSCmdlet.ParameterSetName -eq "endpoint") {
        Write-Information "Retrieving the Microsoft Graph authentication token using the provided endpoint."
        $token = Get-MicrosoftGraphAuthenticationToken -Endpoint $endpoint
        if ([String]::IsNullOrWhiteSpace($token)) {
            Write-OutputMessage "Failed to retrieve the Microsoft Graph authentication token using the provided endpoint." -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
    }

    # Set the protocol to TLS 1.2
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

    # Remove any "\" or "/" at the start of the SharePoint Online file path
    if ($filePath.StartsWith("\")) {
        $filePath = $filePath.TrimStart("\")
    }
    if ($filePath.StartsWith("/")) {
        $filePath = $filePath.TrimStart("/")
    }

    # Verify that the file path is valid
    if ([String]::IsNullOrWhiteSpace($filePath)) {
        Write-OutputMessage "Failed to retrieve a valid file path from '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Verify that the siteUrl is valid
    if (!(Test-DomainValidity -Domain $siteUrl)) {
        Write-OutputMessage "The siteUrl '$($siteUrl)' is not valid." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Get the url to download the file
    $downloadUrl = Get-SharePointOnlineFileDownloadLink -FilePath $filePath -SiteUrl $siteUrl -Token $token
    if ([String]::IsNullOrWhiteSpace($downloadUrl)) {
        Write-OutputMessage "Failed to retrieve the link to download '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # Download the file
    Write-Information "Downloading '$($filePath)' from SharePoint Online to $($destinationFilePath)."
    $destinationPath = Get-FileFromUrl -FileUrl $downloadUrl -DestinationPath $destinationFilePath

    # Successfully downloaded the file
    if ($destinationPath) {
        return $true
    }

    # Failed to download the file
    else {
        Write-OutputMessage "Failed to download '$($filePath)' to '$($destinationFilePath)'." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }
}