.bat文件中的powershell命令失败

时间:2018-07-23 09:41:53

标签: powershell batch-file

嗨,我在bat文件中运行了powershell命令,并遇到以下错误:

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')"

错误:

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

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

请参阅:Powershell Setting Security Protocol to Tls 1.2 和 :Invoke-WebRequest SSL fails?

您可以使用批处理文件执行以下操作:

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

您还可以使用Certutil命令下载带有这样的批处理文件的文件:

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