Functions/Upload-FileToSharePointOnline.ps1

<#
.SYNOPSIS
    This function uploads a file to SharePoint Online.
.DESCRIPTION
    This function uploads a file to SharePoint Online.
    The method used to upload the file depends on the size of the file.
    If the file is less than 4MB in size, Upload-SmallFileToSharePointOnline is used.
    If it is between 4 MB and 60MB in size, Upload-LargeFileToSharePointOnline is used.
    Otherwise, the file will not be uploaded.
#>

function Upload-FileToSharePointOnline {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param (
        # The path to the file on the local machine.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$filePath,

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

        # The User Principal Name of the user whose SharePoint Online account will receive the file.
        [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",

        # If selected, this function will overwrite the existing file on SharePoint Online.
        [Parameter(Mandatory=$false)]
        [Switch]$overwrite = [Switch]::Present
    )

    # Verify that the file exists on the local machine
    if (!(Test-Path -Path $filePath -ErrorAction SilentlyContinue)) {
        Write-OutputMessage "The file '$($filePath)' on the local machine cannot be found." -OutputStream $outputStream -ReturnMessage:$false
        return $false
    }

    # 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 destination path
    if ($destinationFilePath.StartsWith("\")) {
        $destinationFilePath = $destinationFilePath.TrimStart("\")
    }
    if ($destinationFilePath.StartsWith("/")) {
        $destinationFilePath = $destinationFilePath.TrimStart("/")
    }

    # 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
    }

    # Verify if the file already exists on SharePoint Online
    Write-Information "Verifying if the file '$($destinationFilePath)' already exists on SharePoint Online."
    $downloadUrl = Get-SharePointOnlineFileDownloadLink -FilePath $destinationFilePath -SiteUrl $siteUrl -Token $token -OutputStream "None"

    if (!$downloadUrl) {
        Write-Information "The file '$($destinationFilePath)' does not exist on SharePoint Online."
    }
    else {
        if (!$overwrite) {
            Write-OutputMessage "The file '$($destinationFilePath)' already exists on SharePoint Online." -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
        else {
            Write-Warning "The file '$($destinationFilePath)' already exists on SharePoint Online - it will be overwritten."
        }
    }

    # Call the appropriate function depending on the size of the file
    $file = Get-Item -Path $filePath

    # Use Upload-SmallFileToSharePoint Online if the file size is smaller than 4MB
    if ($file.Length -le 4194304) {
        Write-Information "Uploading the file '$($filePath)' to SharePoint Online '$($destinationFilePath)' using the small file upload."
        $result = Upload-SmallFileToSharePointOnline -FilePath $filePath -DestinationFilePath $destinationFilePath `
            -siteUrl $siteUrl -Token $token -OutputStream $outputStream
        if (!$result) {
            Write-OutputMessage "Failed to upload the file '$($filePath)' to SharePoint Online '$($destinationFilePath)'." -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
    }

    # Use Upload-LargeFileToSharePoint Online if the file size is smaller than 60MB
    elseif ($file.Length -le 62914560) {
        Write-Information "Uploading the file '$($filePath)' to SharePoint Online '$($destinationFilePath)' using the large file upload."
        $result = Upload-LargeFileToSharePointOnline -FilePath $filePath -DestinationFilePath $destinationFilePath `
            -siteUrl $siteUrl -Token $token -OutputStream $outputStream
        if (!$result) {
            Write-OutputMessage "Failed to upload the file '$($filePath)' to SharePoint Online '$($destinationFilePath)'." -OutputStream $outputStream -ReturnMessage:$false
            return $false
        }
    }

    # Only accept files less than 60 MB
    else {
        Write-Information "The actual file size is $("{0:n2}" -f ($file.Length / 1048576)) MB. The maximum file size allowed is 60 MB."
        return $false
    }

    # Successfully uploaded the file
    return $true
}