Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Invoke-WebRequest, how to automatically use original file name?

Tags:

How can I use Invoke-WebRequest to download a file but automatically make the file name the same as if I downloaded via browser? I haven't found a way to make -OutFile work without manually specifying the file name. I'm fine with this involving a few other lines of code.

A good solution will:

  • Work even if the file name isn't in the request URL. For example, the URL to download the Visual Studio x64 Remote Debugging Tools is http://go.microsoft.com/fwlink/?LinkId=393217 but it downloads the file rtools_setup_x64.exe.
  • Not save the whole file to memory before writing to disk, unless that's what Invoke-WebRequest already does even with the -OutFile parameter (?)

Thanks!

like image 374
Vimes Avatar asked Aug 04 '14 19:08

Vimes


People also ask

What is the difference between invoke-RestMethod and invoke-WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.

Does invoke-WebRequest use proxy?

Beginning in PowerShell 7.0, Invoke-WebRequest supports proxy configuration defined by environment variables.

What is PowerShell invoke-WebRequest?

Description. The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements. This cmdlet was introduced in Windows PowerShell 3.0.

What is invoke-RestMethod in PowerShell?

Description. The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes.


2 Answers

For the example given you're going to need to get the redirected URL, which includes the file name to be downloaded. You can use the following function to do so:

Function Get-RedirectedUrl {

    Param (
        [Parameter(Mandatory=$true)]
        [String]$URL
    )

    $request = [System.Net.WebRequest]::Create($url)
    $request.AllowAutoRedirect=$false
    $response=$request.GetResponse()

    If ($response.StatusCode -eq "Found")
    {
        $response.GetResponseHeader("Location")
    }
}

Then it's a matter of parsing the file name from the end of the responding URL (GetFileName from System.IO.Path will do that):

$FileName = [System.IO.Path]::GetFileName((Get-RedirectedUrl "http://go.microsoft.com/fwlink/?LinkId=393217"))

That will leave $FileName = rtools_setup_x64.exe and you should be able to download your file from there.

like image 165
TheMadTechnician Avatar answered Sep 24 '22 02:09

TheMadTechnician


Thanks to Ryan I have a semi-usable function:

Function Get-Url {
  param ( [parameter(position=0)]$uri )
  invoke-webrequest -uri "$uri" -outfile $(split-path -path "$uri" -leaf)
}

A graphic file and xml file I have been able to download. When I try to download this webpage and open it with Edge it will work at times.

like image 41
Todd Partridge Avatar answered Sep 23 '22 02:09

Todd Partridge