簡體   English   中英

.bat文件中的powershell命令失敗

[英]powershell command fails in .bat 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

有什么建議么?

請參考: Powershell將安全協議設置為Tls 1.2,以及: Invoke-WebRequest SSL失敗?

您可以使用批處理文件執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM