PowerShell 7.3 - scenarios with Concurrent File Downloads and Handling Exceptions
I just started working with I tried several approaches but none seem to work... I've searched everywhere and can't find a clear answer. I've searched everywhere and can't find a clear answer. I'm trying to implement a script in PowerShell 7.3 to download multiple files concurrently from a list of URLs, but I'm working with issues with behavior handling when some downloads unexpected result. I'm using `Invoke-WebRequest` within a `ForEach-Object` loop to perform the downloads, but when an behavior occurs, it seems to stop the whole process instead of just logging the behavior and continuing with the next file. Here's a snippet of my code: ```powershell $urls = @( 'https://example.com/file1.zip', 'https://example.com/file2.zip', 'https://example.com/nonexistent.zip' ) $destinationFolder = 'C:\Downloads' $urls | ForEach-Object -Parallel { try { $fileName = Split-Path $_ -Leaf Invoke-WebRequest -Uri $_ -OutFile "$using:destinationFolder\$fileName" Write-Host "Downloaded: $fileName" } catch { Write-Host "Failed to download $_: $_.Exception.Message" } } -ThrottleLimit 4 ``` When I run this, if one of the URLs is invalid (like 'https://example.com/nonexistent.zip'), I get the following behavior: `Invoke-WebRequest : Exception calling "DownloadFile" with "2" argument(s): "404 Not Found"`. This causes the entire script to terminate, and I don't see any logs for the successful downloads either. I've tried using `$ErrorActionPreference = 'Continue'` at the start of the script, but it doesn't seem to have an effect in this context. I want to ensure all downloads are attempted and any failures are logged without stopping the script. Any suggestions on how to improve behavior handling in this concurrent download scenario? I'm working on a CLI tool that needs to handle this. Am I missing something obvious? For context: I'm using Powershell on macOS. Am I missing something obvious? I'm working with Powershell in a Docker container on CentOS. Any suggestions would be helpful.