Powershell Download via HTTP using Proxy and Checking Remote File Size Fails on Second File
Solution 1:
I can't comment on this particular issue per se, but I'd like to offer some general recommendations. I've written some quite mission critical PS code over the last few years, some of which interacts with remote telemetry equipment over HTTPS. This code either has to succeed or fail gracefully, with very verbose logging. The code, although running in Windows VMs, is treated as an "embedded system / appliance".
I'd sincerely recommend adding error-handling code to your script. Granted, the Internet is full of bang-bang-bang examples of PowerShell code, with each successive step assuming that the previous one succeeded. This is sometimes made worse by over-use of PS pipelines, e.g.: enumerate-something | foreach-object{ do-something }, where some of the do-something's fail, meaning you're often left in an impossible roll-back situation.
Anyway, enough babbling. Recommendations:
- Enable strict mode. This'll prevent any unintentional type-casting / coercion. Pre-declare your variables, with correct type definitions.
- Use -erroraction silentlyContinue where possible, and trap errors with "if ($?)" checks... or wrap calls that generate exceptions with try{} catch{} constructs. Not all PS functions raise exceptions!
- Write plenty of log data
- Identify actions that are more likely to fail and code retry logic where possible.
I'll stop there!
Good luck.
Solution 2:
You issue is due to the $test variable being assigned a null value.
When you build the variable $test
$test = $wc2.OpenRead($source) | Out-Null
the "| Out-Null" portion is nulling the results causing null to be assigned to $test, remove it and you should not have an issue.
As an aside for your question on closing System.Net.WebClient's connections: Action on you end is not required, all HTTP connections are closed when required and should not interfere with establishing a second connection. An open persistent connection will allow for a more timely and efficient connection on subsequent connections to the same URL. More information on persistent HTTP connections can be found here.