0
votes

I need to download a file daily from a website where date will passed. I tried using following code -

$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\"
Invoke-WebRequest -Uri $url -OutFile $output

But it is giving error -

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. At line:3 char:1 + Invoke-WebRequest -Uri $url -OutFile $output + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

1
I tried this also - $WebClient = New-Object System.Net.WebClient $WebClient.DownloadFile("theocc.com/webapps/threshold-securities","C:\Users\Himanshu.Vats\Downloads\") and this giving error -Himanshu
Please see this post: stackoverflow.com/questions/41618766/… (ie. try setting [Net.ServicePointManager]::SecurityProtocol = 'Tls11,Tls12' before calling Invoke-WebRequest)Mathias R. Jessen

1 Answers

0
votes

It's because your path is not a path file (you have a '\' to end path)

try this:

$url = "https://www.theocc.com/webapps/threshold-securities?reportDate=20190730"
$output = "C:\Users\Himanshu.Vats\Downloads\result.csv"
Invoke-WebRequest -Uri $url -OutFile $output