title: FTPS connection not closing properly (powershell script)
body: <p>I use <a href="https://gist.github.com/pbrumblay/6551936" rel="nofollow noreferrer">this function</a> to send a file via FTPS with a PowerShell script. It is a Filezilla Server.
  It works fine, the file is written on the server, but the connection is not closed properly.</p>
  <p>I get the following error :</p>
  <pre><code>Exception when calling &quot;Close&quot; with &quot;0&quot; argument(s): &quot;The remote server returned an error: 
  (425) Unable to open data connection.&quot;
  At character C:\...\transfer.ps1:57: 3
  + $ftpStream.Close();
  + ~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
   
  Exception when calling &quot;GetResponse&quot; with &quot;0&quot; argument(s): &quot;The remote server returned an error
  : (425) Cannot open data connection.&quot;
  At character C:\...\transfer.ps1:58 : 3
  + $resp = $req.GetResponse();
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
   
  Impossible to call a method in a Null expression.
  At character C:\...\transfer.ps1:60 : 3
  + $resp.Close();
  + ~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation : (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  </code></pre>
  <p>And on the server side :</p>
  <pre><code>2022-02-08T11:32:42.864Z II [FTP Session 11 147.128.66.87] Session 0x1ab850b0090 with ID 11 created.
  2022-02-08T11:32:42.864Z &gt;&gt; [FTP Session 11 147.128.66.87] AUTH TLS
  2022-02-08T11:32:42.880Z &lt;&lt; [FTP Session 11 147.128.66.87] 234 Using authentication type TLS.
  2022-02-08T11:32:42.880Z II [FTP Session 11 147.128.66.87] TLS Handshake successful
  2022-02-08T11:32:42.880Z II [FTP Session 11 147.128.66.87] Protocol: TLS1.2, Key exchange: ECDHE-X25519-ECDSA-SHA256, Cipher: AES-256-GCM, MAC: AEAD
  2022-02-08T11:32:42.880Z &gt;&gt; [FTP Session 11 147.128.66.87] USER uploaduser
  2022-02-08T11:32:42.880Z &lt;&lt; [FTP Session 11 147.128.66.87] 331 Please, specify the password.
  2022-02-08T11:32:42.880Z &gt;&gt; [FTP Session 11 147.128.66.87] PASS ****
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 230 Login successful.
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] PBSZ 0
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 200 PBSZ=0
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] PROT P
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 200 Protection level set to P
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] OPTS utf8 on
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 202 UTF8 mode is always enabled. No need to send this command
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] PWD
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 257 &quot;/&quot; is current directory.
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] TYPE I
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 200 Type set to I
  2022-02-08T11:32:42.942Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] PASV
  2022-02-08T11:32:42.942Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 227 Entering Passive Mode (147,128,66,56,222,117)
  2022-02-08T11:32:43.021Z &gt;&gt; [FTP Session 11 147.128.66.87 uploaduser] STOR test.txt
  2022-02-08T11:32:43.021Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 150 Starting data transfer.
  2022-02-08T11:32:43.036Z II [FTP Session 11 147.128.66.87 uploaduser] TLS Handshake successful
  2022-02-08T11:32:43.036Z II [FTP Session 11 147.128.66.87 uploaduser] TLS Session resumed
  2022-02-08T11:32:43.036Z II [FTP Session 11 147.128.66.87 uploaduser] Protocol: TLS1.2, Key exchange: ECDHE-X25519, Cipher: AES-256-GCM, MAC: AEAD
  2022-02-08T11:32:43.036Z !! [FTP Session 11 147.128.66.87 uploaduser] GnuTLS error -110 in gnutls_record_recv: The TLS connection was non-properly terminated.
  2022-02-08T11:32:43.036Z == [FTP Session 11 147.128.66.87 uploaduser] Client did not properly shut down TLS connection
  2022-02-08T11:32:43.036Z &lt;&lt; [FTP Session 11 147.128.66.87 uploaduser] 425 Error while transfering data: ECONNABORTED - Connection aborted
  2022-02-08T11:32:43.036Z !! [FTP Session 11 147.128.66.87 uploaduser] GnuTLS error -110 in gnutls_record_recv: The TLS connection was non-properly terminated.
  2022-02-08T11:32:43.036Z == [FTP Session 11 147.128.66.87 uploaduser] Client did not properly shut down TLS connection
  2022-02-08T11:32:43.036Z !! [FTP Session 11 147.128.66.87 uploaduser] Control channel closed with error from source 0. Reason: ECONNABORTED - Connection aborted.
  2022-02-08T11:32:43.036Z !! [FTP Server] Session 11 ended with error from source 0. Reason: ECONNABORTED - Connection aborted.
  2022-02-08T11:32:43.036Z II [FTP Session 11 147.128.66.87] Session 0x1ab850b0090 with ID 11 destroyed.
  </code></pre>
  <p>Tried different things on the server firewall, even disabled it, does not change anything.</p>
  <p>Changed &quot;keep alive&quot; to $False, tried to disable passive mode on both server side and changing in the script to $False, without success.</p>
  <p>Am I missing something ?</p>