gaurav2907
gaurav2907

Reputation: 51

powershell command fails in .bat file

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?

Upvotes: 0

Views: 1288

Answers (1)

Hackoo
Hackoo

Reputation: 18827

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
::------------------------------------------

Upvotes: 2

Related Questions