skip to Main Content

I need a PowerShell script to get latest binary version from this archive list https://download.docker.com/win/static/stable/x86_64/

So as when I run the script, it checks for latest zip and returns me latest version number and link to download. For example, if latest file is docker-24.0.6.zip then I need

  • 24.0.6
  • link to that zip which ultimately I can use to download

I have subsequent script to utilize that zip but I have never tried getting it from an archive url.

2

Answers


  1. docker-check.ps1:

    $archiveUrl = "https://download.docker.com/win/static/stable/x86_64/"
    
    $response = Invoke-WebRequest -Uri $archiveUrl
    
    $pattern = '<a href="([^"]*docker-([d.]+).zip)"'
    $matches = [regex]::Matches($response.Content, $pattern)
    
    $latestVersion = $null
    $latestDownloadLink = $null
    
    foreach ($match in $matches) {
        $downloadLink = $match.Groups[1].Value
        $version = $match.Groups[2].Value
    
        if ($latestVersion -eq $null -or [Version]$version -gt [Version]$latestVersion) {
            $latestVersion = $version
            $latestDownloadLink = $downloadLink
        }
    }
    
    if ($latestVersion -ne $null -and $latestDownloadLink -ne $null) {
        Write-Host "Latest Version: $latestVersion"
        Write-Host "Download Link: $archiveUrl$latestDownloadLink"
    } else {
        Write-Host "Unable to find the latest version and download link."
    }
    

    Output:

    enter image description here

    Login or Signup to reply.
  2. To offer a simpler alternative to Ömer Sezer’s helpful solution, based on the observation that the version list at https://download.docker.com/win/static/stable/x86_64/ is simply being appended to, implying that the most recent recent version is listed last:

    $rootUrl = 'https://download.docker.com/win/static/stable/x86_64/'
    
    # Get the name of the ZIP file containing the latest version, 
    # assumed to be the last one ([-1]) listed.
    $latestVersionZipFile = 
      [regex]::Matches(
        (Invoke-RestMethod -UseBasicParsing $rootUrl), 
        'bdocker-(.+?).zip'
      ).Value[-1]
    
    # Construct the full download URL
    $latestVersionZipFileUrl = $rootUrl + $latestVersionZip
    

    Note: -UseBasicParsing, which prevents parsing of the response by the obsolete Internet Explorer engine, is only required in Windows PowerShell – you can omit the parameter in PowerShell (Core) 7+.

    You can then download the latest version as follows:

    Invoke-RestMethod -UseBasicParsing $latestVersionZipFileUrl -OutFile $latestVersionZipFile
    

    If you don’t want to rely on the last ZIP file listed being the most recent one, replace the $latestZipFile = ... statement with the following, which sorts the extracted filenames by their embedded version number and picks the highest one.

    $latestVersionZipFile = 
      [regex]::Matches(
        (Invoke-RestMethod -UseBasicParsing $rootUrl), 
        'bdocker-(.+?).zip'
      ).Value |
      Sort-Object { [version] ($_ -replace '^docker-|(-ce)?.zip') } |
      Select-Object -Last 1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search