Functions/Download-FileFromOneDrive.ps1
<#
.SYNOPSIS This function downloads a file from OneDrive. .DESCRIPTION. This function downloads a file from OneDrive. #> function Download-FileFromOneDrive { [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 OneDrive. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$filePath, # The User Principal Name of the user whose OneDrive account the file will be downloaded from. [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")] [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 OneDrive 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 UserPrincipalName is valid if (!(Test-EmailAddressValidity -EmailAddress $userPrincipalName)) { Write-OutputMessage "The UserPrincipalName '$($userPrincipalName)' is not valid." -OutputStream $outputStream -ReturnMessage:$false return $false } # Get the url to download the file $downloadUrl = Get-OneDriveFileDownloadLink -FilePath $filePath -UserPrincipalName $userPrincipalName -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 OneDrive 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 } } |