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.
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"
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With