Functions/Get-SharePointOnlineFileDownloadLink.ps1
<#
.SYNOPSIS This function gets the download link of a file from SharePoint Online. .DESCRIPTION This function gets the download link of a file from SharePoint Online. #> function Get-SharePointOnlineFileDownloadLink { [CmdletBinding(PositionalBinding=$false)] [OutputType([String])] param ( # The path of the file on SharePoint Online. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$filePath, # The site Url of the user whose SharePoint Online account 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", "None")] [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 $null } } # 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 $null } # Verify that the siteUrl is valid if (!(Test-DomainValidity -Domain $siteUrl)) { Write-OutputMessage "The siteUrl '$($siteUrl)' is not valid." -OutputStream $outputStream -ReturnMessage:$false return $null } # Prepare the GET Request $invokeRestMethodParams = @{ Uri = "https://graph.microsoft.com/v1.0/sites/$($siteUrl)/drive/root:/$($filePath)" Method = "GET" Headers = @{ Accept = "application/json" Authorization = "bearer $($token)" } } # Try to get the link to download the file from SharePoint Online Write-Information "Retrieving the link to download '$($filePath)' from SharePoint Online." try { $response = Invoke-RestMethod @invokeRestMethodParams } catch { Write-OutputMessage "Exception occurred while retrieving the link to download the file '$($filePath)' from SharePoint Online.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false return $null } # Get the url of the file from the response $downloadUrl = $response.'@microsoft.graph.downloadUrl' if ([String]::IsNullOrWhiteSpace($downloadUrl)) { Write-OutputMessage "Failed to retrieve the link to download '$($filePath)'." -OutputStream $outputStream -ReturnMessage:$false return $null } return $downloadUrl } |