fredrik
fredrik

Reputation: 10281

How to download latest file from web archive, using powershell?

I'd like a powershell script to download the most recent AMD64 installer from web archive.

I have this, where the URL is hard-coded:

$src = 'https://repo.saltstack.com/windows/Salt-Minion-2015.8.5-AMD64-Setup.exe'
$dst = $env:temp+'\Salt-Minion-2015.8.5-AMD64-Setup.exe'

# Download installer
Invoke-WebRequest $src -OutFile $dst

# Install
& $dst /S /master=salt /minion-name=$env:computername /start-service=1

# Remove installer
Remove-Item $dst

How can I retrieve the latest installer and set $src to its URL?

I've figured out how to list all files and folders:

Invoke-WebRequest -UseBasicParsing 'https://repo.saltstack.com/windows' | select -ExpandProperty Links | select href

The output:

...
...
...
Salt-Minion-2015.8.5-AMD64-Setup.exe
Salt-Minion-2015.8.5-AMD64-Setup.exe.md5
Salt-Minion-2015.8.5-x86-Setup.exe
Salt-Minion-2015.8.5-x86-Setup.exe.md5
archive/
dependencies/

...however, how do I extract the last file with AMD64 and not .md5 in the filename?

Upvotes: 0

Views: 2191

Answers (1)

fredrik
fredrik

Reputation: 10281

This works for me:

# Find latest installer
$url = 'https://repo.saltstack.com/windows/'
$site = Invoke-WebRequest -UseBasicParsing -Uri $url
$table = $site.links | ?{ $_.tagName -eq 'A' -and $_.href.ToLower().Contains('amd64') -and $_.href.ToLower().EndsWith("exe") } | sort href -desc | select href -first 1
$filename = $table.href.ToString()

# Download installer
$src = $url + $filename
$dst = $env:temp + '\' + $filename
Invoke-WebRequest $src -OutFile $dst

# Install
& $dst /S /master=salt /minion-name=$env:computername /start-service=1

Upvotes: 0

Related Questions