Functions/Get-FileFromUrl.ps1
<#
.SYNOPSIS This function downloads a file from a URL. #> function Get-FileFromUrl { [CmdletBinding()] [OutputType([System.IO.FileInfo])] param ( # The URL of the file. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$fileUrl, # The destination path for the file. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$destinationPath ) # Test the destination directory path $destinationDirectoryPath = Split-Path -Path $destinationPath -Parent if (!(Test-Path -Path $destinationDirectoryPath -ErrorAction SilentlyContinue)) { Write-Error "Destination directory for downloaded file '$($destinationDirectoryPath)' is invalid." return $null } # Download the file try { $webClient = New-Object System.Net.WebClient $webClient.DownloadFile($fileUrl, $destinationPath) } catch { Write-Error "Error while to downloading file from '$($fileUrl)'.`r`n$($_.Exception.Message)" return $null } if (!(Test-Path $destinationPath)) { Write-Error "Failed to download file from '$($fileUrl)'." return $null } # Success, return the file return Get-Item -Path $destinationPath } |