Functions/Upload-FileToOneDrive.ps1
<#
.SYNOPSIS This function uploads a file to OneDrive. .DESCRIPTION. This function uploads a file to OneDrive. The method used to upload the file depends on the size of the file. If the file is less than 4MB in size, Upload-SmallFileToOneDrive is used. If it is between 4 MB and 60MB in size, Upload-LargeFileToOneDrive is used. Otherwise, the file will not be uploaded. #> function Upload-FileToOneDrive { [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 OneDrive. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$destinationFilePath, # The User Principal Name of the user whose OneDrive account will receive the file. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$userPrincipalName, # 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", "None")] [String]$outputStream = "Error", # If selected, this function will overwrite the existing file on OneDrive. [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 OneDrive destination path if ($destinationFilePath.StartsWith("\")) { $destinationFilePath = $destinationFilePath.TrimStart("\") } if ($destinationFilePath.StartsWith("/")) { $destinationFilePath = $destinationFilePath.TrimStart("/") } # Verify that the UserPrincipalName is valid if (!(Test-EmailAddressValidity -EmailAddress $userPrincipalName)) { Write-OutputMessage "The UserPrincipalName '$($userPrincipalName)' is not valid." -OutputStream $outputStream -ReturnMessage:$false return $false } # Verify if the file already exists on OneDrive Write-Information "Verifying if the file '$($destinationFilePath)' already exists on OneDrive." $downloadUrl = Get-OneDriveFileDownloadLink -FilePath $destinationFilePath -UserPrincipalName $userPrincipalName -Token $token -OutputStream "None" if (!$downloadUrl) { Write-Information "The file '$($destinationFilePath)' does not exist on OneDrive." } else { if (!$overwrite) { Write-OutputMessage "The file '$($destinationFilePath)' already exists on OneDrive." -OutputStream $outputStream -ReturnMessage:$false return $false } else { Write-Warning "The file '$($destinationFilePath)' already exists on OneDrive - it will be overwritten." } } # Call the appropriate function depending on the size of the file $file = Get-Item -Path $filePath # Use Upload-SmallFileToOneDrive if the file size is smaller than 4MB if ($file.Length -le 4194304) { Write-Information "Uploading the file '$($filePath)' to OneDrive '$($destinationFilePath)' using the small file upload." $result = Upload-SmallFileToOneDrive -FilePath $filePath -DestinationFilePath $destinationFilePath ` -UserPrincipalName $userPrincipalName -Token $token -OutputStream $outputStream if (!$result) { Write-OutputMessage "Failed to upload the file '$($filePath)' to OneDrive '$($destinationFilePath)'." -OutputStream $outputStream -ReturnMessage:$false return $false } } # Use Upload-LargeFileToOneDrive if the file size is smaller than 60MB elseif ($file.Length -le 62914560) { Write-Information "Uploading the file '$($filePath)' to OneDrive '$($destinationFilePath)' using the large file upload." $result = Upload-LargeFileToOneDrive -FilePath $filePath -DestinationFilePath $destinationFilePath ` -UserPrincipalName $userPrincipalName -Token $token -OutputStream $outputStream if (!$result) { Write-OutputMessage "Failed to upload the file '$($filePath)' to OneDrive '$($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 } |