powershell命令在.bat文件中失败

时间:2021-09-12 16:03:12

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

嗨我在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')"

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?

1 个解决方案

#1


1  

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

请参阅:Powershell设置安全协议到Tls 1.2和此:Invoke-WebRequest SSL失败?

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 :

您还可以使用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
::------------------------------------------

#1


1  

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

请参阅:Powershell设置安全协议到Tls 1.2和此:Invoke-WebRequest SSL失败?

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 :

您还可以使用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
::------------------------------------------