简体   繁体   中英

Downloading/upload files from ftp with powershell from virtualbox

Hello i'm trying to upload files to my virtual machine that run linux and ftp server. I have an exception in the code:

    Exception calling "GetRequestStream" with "0" argument(s): "The requested FTP command is not supported when using HTTP proxy."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:1
+ $Run = $FTPRequest.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

And the script i used is this:

    # Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# Create FTP Rquest Object

    $FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
    $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()

The powershell ver is 5.1. I have proxy on the host machine.

Edit: I'm using the download command now:

# Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# 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)

But atm i have this error:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:16 char:1
+ $FTPResponse = $FTPRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

Second edit:

# Load WinSCP .NET assembly
Add-Type -Path "D:\Users\h.yordanov\Desktop\PowerShellFtp-master\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "192.168.56.101"
    UserName = "sammy"
    Password = "1111"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "1")
$sessionOptions.AddRawSettings("ProxyHost", "proxy.abc.dfg.bg")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\test2.txt"
    $RemoteFile = "/home/sammy/ftp/files/test2.txt"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

After this i get this error:

Exception calling "Open" with "1" argument(s): "Connection failed.
Can't connect to proxy server
No connection could be made because the target machine actively refused it.
Connection failed."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

Edit 3:

Log from connecting via filezilla. (I changed the ip)

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 "/" 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 "/" successful
2017-06-23 14:50:16 6364 1 Status: Retrieving directory listing of "/files"...
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 "/files" 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 "/files" successful
2017-06-23 14:50:18 6364 1 Status: Retrieving directory listing of "/test"...
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 "/test" 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 "/test" 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 "/files" 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

I dont see any proxy connection here.

The FtpWebRequest does not support an HTTP proxy for some operations, including file upload. It's clearly documented on MSDN :

If the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported.

There's no way to upload a file to FTP over an HTTP proxy with PowerShell and the standard .NET framework libraries.

You have to use a 3rd party FTP library or PowerShell module.


For example with WinSCP .NET assembly , you can use:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "mypassword"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "3")
$sessionOptions.AddRawSettings("ProxyHost", "proxy")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
    $RemoteFile = "/files/1618716-maybach-exelero.jpg"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

For the options for SessionOptions.AddRawSettings , see raw settings .

You can have WinSCP GUI generate a PowerShell FTP upload code template , like above, for you.

(I'm the author of WinSCP)


Though your actual problem is that you have proxy configured in your Internet connection options on your machine, while you actually do not want to use them.

The correct solution is to fix the options.
Go to Control panel > Network and Internet > Internet Options > Connections > LAN settings .

Or as workaround, disable the proxy for a particular connection in your PowerShell code:

$FTPRequest.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()

See powershell http get not use proxy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM