我正在通过ps1脚本将war文件传输到Tomcat Web服务器。通过Windows PowerShell ISE执行脚本时,文件的传输有效:
StatusCode : 200 StatusDescription : OK Content : OK - Deployed application at context path /test RawContent : HTTP/1.1 200 OK Transfer-Encoding: chunked Cache-Control: private Content-Type: text/plain;charset=utf-8 Date: Tue, 05 Sep 2017 10:13:24 GMT Expires: Thu, 01 Jan 1970 01:00:00 CET Server:... Forms : Headers : {[Transfer-Encoding, chunked], [Cache-Control, private], [Content-Type, text/plain;charset=utf-8], [Date, Tue, 05 Sep 2017 10:13:24 GMT]...} Images : {} InputFields : {} Links : {} ParsedHtml : RawContentLength : 56
现在,当我从Windows PowerShell以提升模式运行相同的脚本时:
powershell.exe -File .\transfer_war_file.ps1 -ExecutionPolicy Bypass
然后我看
Status : SendFailure Response : Message : The underlying connection was closed: An unexpected error occurred on a send. Data : {} InnerException : System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.MultipleWrite(BufferOffsetSize[] buffers) at System.Net.Security._SslStream.StartWriting(SplitWritesState splitWrite, SplitWriteAsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessWrite(BufferOffsetSize[] buffers, SplitWriteAsyncProtocolRequest asyncRequest) at System.Net.TlsStream.MultipleWrite(BufferOffsetSize[] buffers) at System.Net.ConnectStream.ResubmitWrite(ConnectStream oldStream, Boolean suppressWrite) TargetSite : System.Net.WebResponse GetResponse(System.Net.WebRequest) StackTrace : at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request) at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord() HelpLink : Source : Microsoft.PowerShell.Commands.Utility HResult : -2146233079
代码是
Set-PSDebug -Trace 2
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$user = "test"
$pass = "test"
$secpass = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user, $secpass)
try {
(Invoke-WebRequest -InFile "test.war" -URI "https://somehost:443/manager/text/deploy?path=/test" -Method PUT -Credential $credential -ContentType 'application/zip' -UseBasicParsing)
} catch {
echo $_.Exception | Format-List -Force
}