I am transferring a war file to a Tomcat web server via a ps1 script. The transfer of the file works when I execute the script via 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
Now, when I run the same script from Windows PowerShell in elevated mode:
powershell.exe -File .\transfer_war_file.ps1 -ExecutionPolicy Bypass
Then I see
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
Code is
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
}