使用PowerShell从virtualbox下载/上传ftp文件

时间:2017-06-23 07:38:00

标签: powershell ftp

您好我正在尝试将文件上传到运行linux和ftp服务器的虚拟机。我在代码中有一个例外:

    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

我使用的脚本是:

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

powershell ver是5.1。我在主机上有代理。

编辑:     我现在正在使用下载命令:

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

但是我有这个错误:

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

第二次编辑:

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

在此之后我收到此错误:

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

编辑3:

通过filezilla连接日志。 (我改变了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

我在这里看不到任何代理连接。

1 个答案:

答案 0 :(得分:0)

FtpWebRequest不支持某些操作的HTTP代理,包括文件上传。它显然是documented on MSDN

  

如果指定的代理是HTTP代理,则仅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令。

使用PowerShell和标准.NET框架库,无法通过HTTP代理将文件上传到FTP。

您必须使用第三方FTP库或PowerShell模块。

例如,使用WinSCP .NET assembly,您可以使用:

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

有关SessionOptions.AddRawSettings的选项,请参阅raw settings

您可以拥有WinSCP GUI generate a PowerShell FTP upload code template,如上所述。

(我是WinSCP的作者)

虽然您的实际问题是您在计算机上的Internet连接选项中配置了代理,但实际上您并不想使用它们。

正确的解决方案是修复选项 转到控制面板>网络和互联网>互联网选项>连接>局域网设置

或者作为解决方法,在PowerShell代码中禁用特定连接的代理:

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

请参阅powershell http get not use proxy