timsstuff

Get-WebFile.ps1

Nov 6th, 2016
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [Parameter(Mandatory=$true)][string]$url,
  4.     [Parameter()][string]$dest='.\',
  5.     [Parameter()][switch]$UseBITS=$false
  6.     )
  7.  
  8. $start = Get-Date
  9.  
  10. if($dest.Substring($dest.Length-1, 1) -ne '\') { $dest +='\' }
  11. if(!(Test-Path $dest)) { New-Item $dest -type directory -Force }
  12.  
  13.  
  14. $file = [System.IO.Path]::GetFilename([System.Net.WebUtility]::UrlDecode($url)).Replace('&','&')
  15. if(!(Test-Path -LiteralPath $dest$file)) {
  16.     Write-Host "Getting $file"
  17.     if($UseBITS) {
  18.         Import-Module BitsTransfer
  19.         Start-BitsTransfer -Source $url -Destination $dest$file
  20.     } else {
  21.         Invoke-WebRequest $url -outfile $dest$file -ErrorAction SilentlyContinue
  22.     }
  23. }
  24.  
  25. [gc]::collect()
  26. $end = Get-Date
  27. $elapsed = $end - $start
  28.  
  29. Write-Host "Done! $elapsed" -ForegroundColor Green
Add Comment
Please, Sign In to add comment