Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

downloading multiple files with "Invoke-WebRequest"

Tags:

powershell

So i've just begun to play around with powershell and i'm trying to find a way to pass arrays through Invoke-WebRequest so that i can download multiple files. I realize it can only handle strings, so this is what i've come up with.

$urls = Get-Content .\urls.txt
$outputs = Get-Content .\outputs.txt
foreach ($url in $urls) {foreach ($output in $outputs){Invoke-WebRequest -Uri $url -OutFile $output}}

Now, this snippet works, but since i've nested two foreach'es, it downloads the content twice.

I want it to the same thing it's doing now, but without the 'double download'.

I appreciate any help i can get.

like image 826
kåre KAEE Avatar asked Nov 26 '25 12:11

kåre KAEE


2 Answers

You could create a CSV with the mappings you want between URLs and Outputs, then loop through that:

$csvData = Import-Csv url-and-outputs.csv

foreach($row in $csvData){
    Invoke-WebRequest -Uri $row.url -OutFile $row.output
}

Your CSV file will need the top row to be the column names, i.e. "url" and "output"

like image 108
Charlie Joynt Avatar answered Nov 28 '25 10:11

Charlie Joynt


Just to post an alternative to Charlie's answer, you could also use hash tables to create the association between your URL's and output directories. This will prevent you from having unexpected results when modifying either .txt file. In practice its very similar to the CSV solution, however the hash table could be maintained in the script or generated based on the contents of your two text files (all the keys from one file and all the values from the other).

$myHash = @{
    'www.url1.com/file.log' = 'c:\temp\output1.log';
    'www.url2.com/file.log' = 'd:\myotherdrive\output.log';
}

ForEach ($file in $myHash.GetEnumerator()) {
    Invoke-WebRequest -Uri $file.Key -OutFile $file.Value
}
like image 35
R. Murray Avatar answered Nov 28 '25 10:11

R. Murray