title: Downloading/upload files from FTP with PowerShell from VirtualBox
body: <p>I'm trying to upload files to my virtual machine that run Linux and FTP server. I have an exception in the code:</p>
  <pre class="lang-none prettyprint-override"><code>Exception calling &quot;GetRequestStream&quot; with &quot;0&quot; argument(s): &quot;The requested FTP command is not supported when using HTTP proxy.&quot;
  At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:1
  + $Run = $FTPRequest.GetRequestStream()
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : InvalidOperationException
  </code></pre>
  <p>And the script I used is this:</p>
  <pre><code># Config
  $Username = &quot;sammy&quot;
  $Password = &quot;1111&quot;
  $LocalFile = &quot;C:\fff\medias\1618716-maybach-exelero.jpg&quot;
  $RemoteFile = &quot;ftp://192.168.56.101/files/1618716-maybach-exelero.jpg&quot;
  
  # Create FTP Rquest Object
  
  $FTPRequest = [System.Net.FtpWebRequest]::Create(&quot;$RemoteFile&quot;)
  $FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
  $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
      
  $FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
  $FTPRequest.UseBinary = $true
  $FTPRequest.UsePassive = $true
  # Read the File for Upload
  $FileContent = gc -en byte $LocalFile
  $FTPRequest.ContentLength = $FileContent.Length
  # Get Stream Request by bytes
  $Run = $FTPRequest.GetRequestStream()
  $Run.Write($FileContent, 0, $FileContent.Length)
  # Cleanup
  $Run.Close()
  
  $Run.Dispose()
  </code></pre>
  <p>The PowerShell ver is 5.1. I have proxy on the host machine.</p>
  <hr />
  <p>When trying to download:</p>
  <pre><code># Config
  $Username = &quot;sammy&quot;
  $Password = &quot;1111&quot;
  $LocalFile = &quot;C:\fff\medias\1618716-maybach-exelero.jpg&quot;
  $RemoteFile = &quot;ftp://192.168.56.101/files/1618716-maybach-exelero.jpg&quot;
  
  # Create a FTPWebRequest 
  $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
  $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
  $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
  $FTPRequest.UseBinary = $true 
  $FTPRequest.KeepAlive = $false
  # Send the ftp request
  $FTPResponse = $FTPRequest.GetResponse() 
  # Get a download stream from the server response 
  $ResponseStream = $FTPResponse.GetResponseStream() 
  # Create the target file on the local system and the download buffer 
  $LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
  [byte[]]$ReadBuffer = New-Object byte[] 1024 
  # Loop through the download 
  do { 
      $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
      $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
  } 
  while ($ReadLength -ne 0)
  </code></pre>
  <p>I have this error:</p>
  <pre class="lang-none prettyprint-override"><code>Exception calling &quot;GetResponse&quot; with &quot;0&quot; argument(s): &quot;The remote server returned an error: (403) Forbidden.&quot;
  At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:16 char:1
  + $FTPResponse = $FTPRequest.GetResponse()
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException
  </code></pre>
  <hr />
  <p>When trying WinSCP .NET assembly:</p>
  <pre><code># Load WinSCP .NET assembly
  Add-Type -Path &quot;D:\Users\h.yordanov\Desktop\PowerShellFtp-master\WinSCPnet.dll&quot;
  
  # Set up session options
  $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
      Protocol = [WinSCP.Protocol]::Ftp
      HostName = &quot;192.168.56.101&quot;
      UserName = &quot;sammy&quot;
      Password = &quot;1111&quot;
  }
  
  # Configure proxy
  $sessionOptions.AddRawSettings(&quot;ProxyMethod&quot;, &quot;1&quot;)
  $sessionOptions.AddRawSettings(&quot;ProxyHost&quot;, &quot;proxy.abc.dfg.bg&quot;)
  
  $session = New-Object WinSCP.Session
  
  try
  {
      # Connect
      $session.Open($sessionOptions)
  
      # Upload file
      $LocalFile = &quot;C:\fff\medias\test2.txt&quot;
      $RemoteFile = &quot;/home/sammy/ftp/files/test2.txt&quot;
      $session.PutFiles($LocalFile, $RemoteFile).Check()
  }
  finally
  {
      $session.Dispose()
  }
  </code></pre>
  <p>I get this error:</p>
  <pre class="lang-none prettyprint-override"><code>Exception calling &quot;Open&quot; with &quot;1&quot; argument(s): &quot;Connection failed.
  Can't connect to proxy server
  No connection could be made because the target machine actively refused it.
  Connection failed.&quot;
  At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:5
  +     $session.Open($sessionOptions)
  +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : SessionRemoteException
  </code></pre>
  <hr />
  <p>Log from connecting via FileZilla. (I changed the ip)</p>
  <pre class="lang-none prettyprint-override"><code>2017-06-23 14:50:14 6364 1 Status: Connecting to 192.168.81.3:21...
  2017-06-23 14:50:14 6364 1 Status: Connection established, waiting for welcome message...
  2017-06-23 14:50:14 6364 1 Response: 220 (vsFTPd 3.0.3)
  2017-06-23 14:50:14 6364 1 Command: AUTH TLS
  2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
  2017-06-23 14:50:14 6364 1 Command: AUTH SSL
  2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
  2017-06-23 14:50:14 6364 1 Status: Insecure server, it does not support FTP over TLS.
  2017-06-23 14:50:14 6364 1 Command: USER sammy
  2017-06-23 14:50:14 6364 1 Response: 331 Please specify the password.
  2017-06-23 14:50:14 6364 1 Command: PASS ****
  2017-06-23 14:50:14 6364 1 Response: 230 Login successful.
  2017-06-23 14:50:14 6364 1 Command: SYST
  2017-06-23 14:50:14 6364 1 Response: 215 UNIX Type: L8
  2017-06-23 14:50:14 6364 1 Command: FEAT
  2017-06-23 14:50:14 6364 1 Response: 211-Features:
  2017-06-23 14:50:14 6364 1 Response:  EPRT
  2017-06-23 14:50:14 6364 1 Response:  EPSV
  2017-06-23 14:50:14 6364 1 Response:  MDTM
  2017-06-23 14:50:14 6364 1 Response:  PASV
  2017-06-23 14:50:14 6364 1 Response:  REST STREAM
  2017-06-23 14:50:14 6364 1 Response:  SIZE
  2017-06-23 14:50:14 6364 1 Response:  TVFS
  2017-06-23 14:50:14 6364 1 Response: 211 End
  2017-06-23 14:50:14 6364 1 Status: Server does not support non-ASCII characters.
  2017-06-23 14:50:14 6364 1 Status: Logged in
  2017-06-23 14:50:14 6364 1 Status: Retrieving directory listing...
  2017-06-23 14:50:14 6364 1 Command: PWD
  2017-06-23 14:50:14 6364 1 Response: 257 &quot;/&quot; is the current directory
  2017-06-23 14:50:14 6364 1 Command: TYPE I
  2017-06-23 14:50:14 6364 1 Response: 200 Switching to Binary mode.
  2017-06-23 14:50:14 6364 1 Command: PASV
  2017-06-23 14:50:14 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,192,147).
  2017-06-23 14:50:14 6364 1 Command: LIST
  2017-06-23 14:50:14 6364 1 Response: 150 Here comes the directory listing.
  2017-06-23 14:50:14 6364 1 Response: 226 Directory send OK.
  2017-06-23 14:50:14 6364 1 Status: Directory listing of &quot;/&quot; successful
  2017-06-23 14:50:16 6364 1 Status: Retrieving directory listing of &quot;/files&quot;...
  2017-06-23 14:50:16 6364 1 Command: CWD files
  2017-06-23 14:50:16 6364 1 Response: 250 Directory successfully changed.
  2017-06-23 14:50:16 6364 1 Command: PWD
  2017-06-23 14:50:16 6364 1 Response: 257 &quot;/files&quot; is the current directory
  2017-06-23 14:50:16 6364 1 Command: PASV
  2017-06-23 14:50:16 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,193,132).
  2017-06-23 14:50:16 6364 1 Command: LIST
  2017-06-23 14:50:16 6364 1 Response: 150 Here comes the directory listing.
  2017-06-23 14:50:16 6364 1 Response: 226 Directory send OK.
  2017-06-23 14:50:16 6364 1 Status: Calculating timezone offset of server...
  2017-06-23 14:50:16 6364 1 Command: MDTM test.txt
  2017-06-23 14:50:16 6364 1 Response: 213 20170622155419
  2017-06-23 14:50:16 6364 1 Status: Timezone offset of server is 0 seconds.
  2017-06-23 14:50:16 6364 1 Status: Directory listing of &quot;/files&quot; successful
  2017-06-23 14:50:18 6364 1 Status: Retrieving directory listing of &quot;/test&quot;...
  2017-06-23 14:50:18 6364 1 Command: CWD /test
  2017-06-23 14:50:18 6364 1 Response: 250 Directory successfully changed.
  2017-06-23 14:50:18 6364 1 Command: PWD
  2017-06-23 14:50:18 6364 1 Response: 257 &quot;/test&quot; is the current directory
  2017-06-23 14:50:18 6364 1 Command: PASV
  2017-06-23 14:50:18 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,162,156).
  2017-06-23 14:50:18 6364 1 Command: LIST
  2017-06-23 14:50:18 6364 1 Response: 150 Here comes the directory listing.
  2017-06-23 14:50:18 6364 1 Response: 226 Directory send OK.
  2017-06-23 14:50:18 6364 1 Status: Directory listing of &quot;/test&quot; successful
  2017-06-23 14:50:22 6364 3 Status: Connecting to 192.168.81.3:21...
  2017-06-23 14:50:22 6364 3 Status: Connection established, waiting for welcome message...
  2017-06-23 14:50:22 6364 3 Response: 220 (vsFTPd 3.0.3)
  2017-06-23 14:50:22 6364 3 Command: AUTH TLS
  2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
  2017-06-23 14:50:22 6364 3 Command: AUTH SSL
  2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
  2017-06-23 14:50:22 6364 3 Status: Insecure server, it does not support FTP over TLS.
  2017-06-23 14:50:22 6364 3 Command: USER sammy
  2017-06-23 14:50:22 6364 3 Response: 331 Please specify the password.
  2017-06-23 14:50:22 6364 3 Command: PASS ****
  2017-06-23 14:50:22 6364 3 Response: 230 Login successful.
  2017-06-23 14:50:22 6364 3 Status: Server does not support non-ASCII characters.
  2017-06-23 14:50:22 6364 3 Status: Logged in
  2017-06-23 14:50:22 6364 3 Status: Starting download of /files/test.txt
  2017-06-23 14:50:22 6364 3 Command: CWD /files
  2017-06-23 14:50:22 6364 3 Response: 250 Directory successfully changed.
  2017-06-23 14:50:22 6364 3 Command: PWD
  2017-06-23 14:50:22 6364 3 Response: 257 &quot;/files&quot; is the current directory
  2017-06-23 14:50:23 6364 3 Command: TYPE A
  2017-06-23 14:50:23 6364 3 Response: 200 Switching to ASCII mode.
  2017-06-23 14:50:23 6364 3 Command: PASV
  2017-06-23 14:50:23 6364 3 Response: 227 Entering Passive Mode (192,168,81,3,162,251).
  2017-06-23 14:50:23 6364 3 Command: RETR test.txt
  2017-06-23 14:50:23 6364 3 Response: 150 Opening BINARY mode data connection for test.txt (17 bytes).
  2017-06-23 14:50:23 6364 3 Response: 226 Transfer complete.
  2017-06-23 14:50:23 6364 3 Status: File transfer successful, transferred 17 bytes in 1 second
  </code></pre>
  <p>I don't see any proxy connection here.</p>