document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # Code Snippet from aperturescience.su
  2.  
  3. Function Get-WebFile
  4. {
  5. <#
  6. .SYNOPSIS
  7. Gets a file off the interwebs.
  8.  
  9. .DESCRIPTION
  10. Gets the file at the specified URL and saves it to the hard disk. Files can be accessed over an URI including http://, Https://, file://,  ftp://, \\\\server\\folder\\file.txt etc.
  11.  
  12. Specification of the destination filename, and/or directory that file(s) will be saved to is supported.
  13. If no directory is supplied, files downloaded to current directory.
  14. If no filename is specified, files downnloaded with have filenamed based on URL, eg http://live.sysinternals.com/procexp.exe downloaded to proxecp.exe, http://google.com downloaded to google.com
  15.  
  16. By default if a file already exists at the specified location, an exception will be generated and execution terminated.
  17. Will pass any errors encountered up to caller!
  18.  
  19. .PARAMETER URL
  20. [Pipeline] The url of the file we want to download. URL must have format like: http://google.com, https://microsoft.com, file://c:\\test.txt
  21.  
  22. .PARAMETER Filename
  23. [Optional] Filename to save file to
  24.  
  25. .PARAMETER Directory
  26. [Optional] Directory to save the file to
  27.  
  28. .PARAMETER Credentials
  29. [Optional] Credentials for remote server
  30.  
  31. .PARAMETER WebProxy
  32. [Optional] Web Proxy to be used, if none supplied, System Proxy settings will be honored
  33.  
  34. .PARAMETER Headers
  35. [Optional] Used to specify additional headers in HTTP request
  36.  
  37. .PARAMETER UserAgent
  38. [Optional] Simpler method of specifying the UserAgent string
  39.  
  40. .PARAMETER clobber
  41. [SWITCH] [Optional] Do we want to overwrite files? Default is to throw error if file already exists.
  42.  
  43. .INPUTS
  44. Accepts strings representing URI to files we want to download from pipeline
  45.  
  46. .OUTPUTS
  47. No output
  48.  
  49. .EXAMPLE
  50. get-webfile "http://live.sysinternals.com/procexp.exe"
  51.  
  52. .EXAMPLE
  53. get-webfile "http://live.sysinternals.com/procexp.exe" -filename "pants.exe"
  54. Download file at url but save as pants.exe
  55.  
  56. .EXAMPLE
  57. gc filelist.txt | get-webfile -directory "c:\\temp"
  58. Where filelist.txt contains a list of urls to download, files downloaded to c:\\temp
  59.  
  60. .NOTES
  61. NAME: Get-WebFile
  62. AUTHOR: kieran@thekgb.su
  63. LASTEDIT: 2012-10-14 9:15:00
  64. KEYWORDS:
  65.  
  66. .LINK
  67. http://aperturescience.su/
  68. #>
  69. [CMDLetBinding()]
  70. param
  71. (
  72.   [Parameter(mandatory=$true, valuefrompipeline=$true)][String] $URL,
  73.   [String] $Filename,
  74.   [String] $Directory,
  75.   [System.Net.ICredentials] $Credentials,
  76.   [System.Net.IWebProxy] $WebProxy,
  77.   [System.Net.WebHeaderCollection] $Headers,
  78.   [String] $UserAgent,
  79.   [switch] $Clobber
  80. )
  81.  
  82. Begin
  83. {
  84.     #make a webclient object
  85.     $webclient = New-Object Net.WebClient
  86.    
  87.     #set the pass through variables if they are not null
  88.     if ($Credentials)
  89.     {
  90.         $webclient.credentials = $Credentials
  91.     }
  92.     if ($WebProxy)
  93.     {
  94.         $webclient.proxy = $WebProxy
  95.     }
  96.     if ($Headers)
  97.     {
  98.         $webclient.headers.add($Headers)
  99.     }
  100.     if ($UserAgent)
  101.     {
  102.         $webclient.headers["User-Agent"] = $UserAgent
  103.     }
  104. }
  105.  
  106. Process
  107. {
  108.     #destination to download file to
  109.     $Destination = ""
  110.    
  111.     <#
  112.         This is a very complicated bit of code, but it handles all of the possibilities for the filename and directory parameters
  113.        
  114.         1) If both are specified -> join the two together
  115.         2) If no filename or destination directory is specified -> the destination is the current directory (converted from .) joined with the "leaf" part of the url
  116.         3) If no filename is specified, but a directory is -> the destination is the specified directory joined with the "leaf" part of the url
  117.         4) If filename is specified but a directory is not -> The destination  is the current directory (converted from .) joined with the specified filename
  118.     #>
  119.     if (($Filename -ne "") -and ($Directory -ne ""))
  120.     {
  121.         $Destination = Join-Path $Directory $Filename
  122.     }
  123.     elseif ((($Filename -eq $null) -or ($Filename -eq "")) -and (($Directory -eq $null) -or ($Directory -eq "")))
  124.     {
  125.         $Destination = Join-Path (Convert-Path ".") (Split-Path $URL -leaf)
  126.     }
  127.     elseif ((($Filename -eq $null) -or ($Filename -eq "")) -and ($Directory -ne ""))
  128.     {
  129.         $Destination = Join-Path $Directory (Split-Path $URL -leaf)
  130.     }
  131.     elseif (($Filename -ne "") -and (($Directory -eq $null) -or ($Directory -eq "")))
  132.     {
  133.         $Destination = Join-Path (Convert-Path ".") $Filename
  134.     }
  135.        
  136.     <#
  137.         If the destination already exists and if clobber parameter is not specified then throw an error as we don\'t want to overwrite files,
  138.         else generate a warning and continue
  139.     #>
  140.     if (Test-Path $Destination)
  141.     {
  142.         if ($Clobber)
  143.         {
  144.             Write-Warning "Overwritting file"
  145.         }
  146.         else
  147.         {
  148.             throw "File already exists at destination: $destination, specify -Clobber to overwrite"
  149.         }
  150.     }
  151.        
  152.     #try downloading the file, throw any exceptions
  153.     try
  154.     {
  155.         Write-Verbose "Downloading $URL to $Destination"
  156.         $webclient.DownloadFile($URL, $Destination)
  157.     }
  158.     catch
  159.     {
  160.         throw $_
  161.     }
  162. }
  163.  
  164. }
  165.  
  166. # Code Snippet from aperturescience.su
');