Docker/Update-DockerOnServer.ps1

Function Update-BCDevDockerOnServer() {
    # Get Latest Stable version and URL
    $latestZipFile = (Invoke-WebRequest -UseBasicParsing -uri "https://download.docker.com/win/static/stable/x86_64/").Content.split("`r`n") |
                     Where-Object { $_ -like "<a href=""docker-*"">docker-*" } |
                     ForEach-Object { $zipName = $_.Split('"')[1]; [Version]($zipName.SubString(7,$zipName.Length-11).Split('-')[0]) } |
                     Sort-Object | Select-Object -Last 1 | ForEach-Object { "docker-$_.zip" }

    if (-not $latestZipFile) {
        throw "Unable to locate latest stable docker download"
    }
    $latestZipFileUrl = "https://download.docker.com/win/static/stable/x86_64/$latestZipFile"
    $latestVersion = [Version]($latestZipFile.SubString(7,$latestZipFile.Length-11))
    Write-Host "Latest stable available Docker Engine version is $latestVersion"

    # Check existing docker version
    $dockerService = Get-Service docker -ErrorAction SilentlyContinue
    if ($dockerService) {
        if ($dockerService.Status -eq "Running") {
            $dockerVersion = [Version](docker version -f "{{.Server.Version}}")
            Write-Host "Current installed Docker Engine version is $dockerVersion"
            if ($latestVersion -le $dockerVersion) {
                Write-Host "No new Docker Engine available"  -ForegroundColor Green
                Return
            }
            Write-Host "New Docker Engine available" -ForegroundColor Green
        }
        else {
            Write-Host "Docker Service not running" -ForegroundColor Yellow
        }
    }
    else {
        Write-Host "Docker Engine not found" -ForegroundColor Red
        Return
    }

    Write-Host "Updating to new Docker Engine version"

    if ($dockerService) {
        Stop-Service docker -ErrorAction SilentlyContinue
    }

    # Searching Docker Location
    $DockerPath = (Get-WmiObject win32_service | ?{$_.Name -like 'docker'}).pathname
    if ($DockerPath.indexof(" --") -gt 0) {
        $DockerPath = $DockerPath.Substring(0, $DockerPath.indexof(" --"))
    }
    $DockerPath = $DockerPath.Replace('"','')
    $DockerPath = (Get-Item -Path $DockerPath).DirectoryName

    # Download new version
    $tempFolderPath = Join-Path $Env:Temp $(New-Guid)
    New-Item -Type Directory -Path $tempFolderPath | Out-Null
    $tempFile = Join-Path $tempFolderPath 'docker.zip'
    Invoke-WebRequest -UseBasicParsing -Uri $latestZipFileUrl -OutFile $tempFile

    # Expand
    Expand-Archive -Path $tempFile -DestinationPath $tempFolderPath -Force
    if (Test-Path (Join-Path $DockerPath 'docker.exe')) {
        Copy-Item -Path (Join-Path $tempFolderPath 'docker/docker.exe') -Destination $DockerPath -Force
    }
    if (Test-Path (Join-Path $DockerPath 'dockerd.exe')) {
        Copy-Item -Path (Join-Path $tempFolderPath 'docker/dockerd.exe') -Destination $DockerPath -Force
    }
    Remove-Item $tempFile -Force

    # Start Service
    Start-Service docker
}
Export-ModuleMember -Function Update-BCDevDockerOnServer