0

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
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
fk08
  • 69
  • 3
  • 1
    https://stackoverflow.com/questions/41937964/mysticism-invoke-webrequest-working-only-via-ise relates to a similar issue however my URL is hardcoded – fk08 Sep 05 '17 at 10:34
  • 1
    For help with your code: show your code. – Ansgar Wiechers Sep 05 '17 at 10:51
  • I edited my post accordingly. Thanks for the hint. – fk08 Sep 05 '17 at 13:20
  • Do you run powershell and powershell_ise in the same context? Is somehost a remote host and is a proxy required/involved? – vrdse Sep 05 '17 at 18:08
  • I am running powershell and powershell_ise as adminstrator. What I noticed is that running the script via ISE is much faster than via powershell. – fk08 Sep 06 '17 at 06:09

0 Answers0