I've been reading the answers here on downloading files under certain conditions, and everything there is clear, but I have a bit of a problem with the place I am trying to download from.
I would like to download from the button beside .zip
in the bottom left that reads 64 bit
(i.e. this would be the 64-bit .zip portable version of the app). https://code.visualstudio.com/download# . I would like to:
- Find the name of the file that would be downloaded, so that I can compare it to what I already have, then
- Download the file if has a different name.
The problems are that a) I can't see the filename, and b) if I right-click the link and select Copy Link, that is not a downloadable object.
I am trying to do this in PowerShell.
CodePudding user response:
As a one-time step, use your web browser to interactively find the URL that is used to request the download, such as via the
Network
tab of Google Chrome's / Brave's Developer Tools view, which in this case leads to:https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive
Fortunately, this URL itself does not include a specific version number, but only abstractly requests the latest stable download, which means that you can use this URL to programmatically, given that it (likely) won't change.
Then use
Invoke-WebRequest
with-Method Head
to only download the response header, without actually downloading the file yet.You can extract the download file name - which includes the version name, e.g.
VSCode-win32-x64-1.72.2.zip
- from the header and then decide whether a download is required.If so, repeat the
Invoke-WebRequest
call with (implied)-Method Get
, and pass the file name to-OutputFile
Note: Both Invoke-WebRequest
and the underlying .NET APIs have changed between Windows PowerShell (versions up to v5.1) and PowerShell (Core) 7 , so two distinct solutions are presented below:
Windows PowerShell solution:
$hd = Invoke-WebRequest -UseBasicParsing -Method Head 'https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive'
# Extract the file name from the response.
$downloadFileName = $hd.BaseResponse.ResponseUri.Segments[-1]
PowerShell (Core) 7 solution:
$hd = Invoke-WebRequest -Method Head 'https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive'
# Extract the file name from the response.
$downloadFileName = $hd.BaseResponse.RequestMessage.RequestUri.Segments[-1]