anonit

Download a list of files from website using Powershell

Mar 6th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Download-files.ps1
  2.  
  3. ## Downloads files from a website
  4.  
  5. <#
  6.  
  7. Requires:
  8.  
  9. inputfile - text file of files to download, 1 URL per line
  10. outputpath - folder to store downloaded files in
  11.  
  12. EG:
  13.  
  14. Given a text file called listToDownload.txt containing the following:
  15.  
  16. http://www.filesgohere.com/wsus/WSUSMaintenance.zip
  17. http://www.filesgohere.com/greenshot.exe
  18. http://www.filesgohere.com/treesizefree.exe
  19. http://www.filesgohere.com/WSUSMaintenance.zip
  20.  
  21. Running the command:
  22. .\Download-files.ps1 -inputfile listToDownload.txt -outputpath "c:\temp"
  23. will download the 4 files in the text file and save them in c:\temp.  The duplicate file, WSUSMaintenance.zip will have the 2nd copy renamed to "http___www.filesgohere.com__wsusmaintenance.zip"
  24.  
  25. EG: duplicates will be renamed to the full URL, excluding NTFS reserved characters (these will be replaced by underscores ("_").)
  26.  
  27.  
  28. #>
  29.  
  30. param(
  31.     [string]$inputfile=$(throw "-inputfile parameter is required"),
  32.     [string]$outputpath=$(throw "-outputpath parameter is required")
  33. )
  34.  
  35. $filestodownload=get-content $inputfile
  36. $splitoption=[System.StringSplitOptions]::RemoveEmptyEntries
  37. foreach ($webaddress in $filestodownload)
  38. {
  39.     $filenamearray=$webaddress.split("/",$splitoption)
  40.     $filename=$filenamearray[-1]
  41.     $destination = "$outputpath\$filename"
  42.     if (test-path $destination)
  43.     {
  44.         $destination=$webaddress.replace("<","_")
  45.         $destination=$destination.replace(">","_")
  46.         $destination=$destination.replace(":","_")
  47.         $destination=$destination.replace("`"","_")
  48.         $destination=$destination.replace("/","_")
  49.         $destination=$destination.replace("\","_")
  50.         $destination=$destination.replace("|","_")
  51.         $destination=$destination.replace("?","_")
  52.         $destination=$destination.replace("*","_")
  53.         $destination="$outputpath\$destination"
  54.     }
  55.     Invoke-WebRequest $webaddress -OutFile $destination
  56. }
Add Comment
Please, Sign In to add comment