0

Hi I am running a powershell command inside a bat file and am getting the below error:

set "workdir=C:\myproject"
mkdir %workdir%
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://www.python.org/ftp/python/3.6.1/python-3.6.1.exe', '%workdir%\pyinstaller.exe')"

Error :

Exception calling "DownloadFile" with "2" argument(s): "The request was aborted: Could not create SSL/TLS secure channel."
At line:1 char:1
+ (New-Object System.Net.WebClient).DownloadFile('https://www.python.or ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException

Any suggestions?

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
gaurav2907
  • 51
  • 1
  • 9
  • `'%workdir%\pyinstaller.exe'` should be `"%workdir%\pyinstaller.exe"`. It is trying to save to %workdir%\pyinstaller.exe isntead of C:\myproject\pyinstaller.exe – Drew Jul 23 '18 at 09:46
  • 2
    Did you tried to google the error? This is not to complicated... It fails on the download because the webpage uses https, you have to do a few extra steps – Paxz Jul 23 '18 at 09:49
  • I've edited your question and removed the code that wasn't relevant, so you now only have a [mcve]. Including a big block of code isn't a good idea as a lot of people can't be bothered to read through to find the actual issue. – henrycarteruk Jul 23 '18 at 09:57
  • 1
    This looks like the answer to your problem. You should've Googled the error message ;) https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel#41618979 – jurgenb Jul 23 '18 at 10:48

1 Answers1

2

Refer to this : Powershell Setting Security Protocol to Tls 1.2 and this : Invoke-WebRequest SSL fails?

You can do something like this with a batch file :

@echo off
Title Download a file with Powershell
color 0A & Mode 60,3
set "workdir=C:\myproject"
If not exist %workdir% mkdir %workdir%
Set "URL=https://www.python.org/ftp/python/3.6.1/python-3.6.1.exe"
Set "FileLocation=%workdir%\pyinstaller.exe"
echo(
echo    Please wait a while ... The download is in progress ...
Call :Download %URL% %FileLocation%
echo Done
Explorer /n,/select,"%FileLocation%" & Exit
::**************************************************************************
:Download <url> <File>
Powershell.exe ^
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^
(New-Object System.Net.WebClient).DownloadFile('%1','%2')
exit /b
::**************************************************************************

You can also download a file with Certutil command with a batch file like this :

@echo off
Title Download a file with Certutil
color 0A & Mode 60,3
set "workdir=C:\myproject"
If not exist %workdir% mkdir %workdir%
Set "URL=https://www.python.org/ftp/python/3.6.1/python-3.6.1.exe"
Set "FileLocation=%workdir%\pyinstaller.exe"
echo(
echo    Please wait a while ... The download is in progress ...
Call :Download %URL% %FileLocation%
echo Done
Explorer /n,/select,"%FileLocation%" & Exit
::------------------------------------------
:Download <url> <File>
Certutil.exe -urlcache -split -f %1 %2>nul
exit /b
::------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70