I am trying to download several files using Powershell and its Invoke-WebRequest method.
I'm basically looping through several filenames (I know that they are available on the server) and download them.
My problem is that my script works for the first file and fails for every file that follows. When I open one of the later files (.csv`s) there is just some html code in it).
I already read a lot about passing session cookings but I am not sure if this is my problem or how I can do that.
My script so far looks like this:
$httpsUser = 'XXX'
$httpsPass = 'YYY'
foreach ($instrument in 'ivv','ijh','ijr','iwm') {
$Source = 'https://***', `
$instrument, '-en_us.csv' -join ""
$Target = 'C:\User\', `
$instrument, '-en_us.csv' -join ""
$uri = New-Object “System.Uri” “$Source”
$WebClient = [System.Net.HttpWebRequest]::Create($uri)
$webclient.Proxy.Credentials =
[System.Net.CredentialCache]::DefaultNetworkCredentials
$webclient.Credentials =
New-Object System.Net.NetworkCredential($httpsUser,$httpsPass)
Invoke-WebRequest -Uri $Source -OutFile $Target
}
Thank you all and let me know what you think :)