【问题标题】:using PsFtp to upload a file to FTP Powershell使用 PsFtp 将文件上传到 FTP Powershell
【发布时间】:2014-05-15 00:12:29
【问题描述】:

我一直在为这个问题绞尽脑汁,似乎无法解决它。我正在尝试使用 PSFTP 将文件上传到 FTP。

我正在使用的脚本:

#------------------------------------------------------
#local variables

$ftp_server = "SERVERNAME"
$ftp_path = "/FTPPATH/PATH"
$local = "C:\ftp\"
$local_in = Join-Path $local "In"
$local_out = Join-Path $local "Out"
$session = "my_ftp_session"

# set up credentials object
$username = "FFandP"
$password = Get-Content "$local_out\Credentials.txt" | ConvertTo-SecureString -AsPlainText -Force 
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

Set-FTPConnection -Server $ftp_server -Credentials $cred -Session $session -KeepAlive -confirm -UseBinary

Get-ChildItem -Path $local_out |
% {

$ftp_file = "$ftp_path/$($_.Name)" # determine item fullname
Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session -
}

# -------------------------------------------------

我收到的错误:

Add-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server     returned an error: (550) File 
unavailable (e.g., file not found, no access)."
At line:22 char:1
+ Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem

我尝试单独运行 Add-FTPitem 命令,但我得到了同样的错误。

我可以使用 FileZilla 上传到 FTP。我也尝试过删除变量并使用硬编码路径;我得到同样的错误。

有什么想法吗?

【问题讨论】:

    标签: powershell upload ftp


    【解决方案1】:

    @Josh 评论中的答案为我解决了这个问题。使用 -Overwrite 参数运行 Add-FTPItem。

    Add-FTPItem -Path $remotePath -LocaPath $myPath -Overwrite
    

    【讨论】:

      【解决方案2】:

      我花了一点时间才弄清楚这个问题,但这是我的解决方案(我遇到了同样的问题)。

      使用 Add-FTPItem 时,-Path 参数必须包含文件名本身。

      Add-FTPItem 
       -Path "ftp://SomeServer/SomeDir/"
       -LocalPath "C:\SomeFilename.ext"
       -Session $session
      

      所以在你的例子中应该是:

      Add-FTPItem -Path $ftp_path -LocalPath $_.FullName -Session $session
      

      文件名将被添加到远程 FTP 路径。如果您不想使用相同的名称,则必须先在本地重命名文件,或者在之后远程重命名文件。

      【讨论】:

      • 为我解决这个问题的是指定“-Overwrite”参数。当 Overwrite 设置为 false(默认值)时,它将首先检查文件是否存在,当文件不存在时,cmdlet 会抛出错误,因为事件未正确处理。您可以通过指定“-Verbose”参数来查看其中的一些内容。
      【解决方案3】:

      更改块

      Get-ChildItem -Path $local_out | %{ .... }
      

      一行

      Get-ChildItem -Path $local_out |  Add-FTPItem  -Path $ftp_path
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 2013-02-22
        • 1970-01-01
        • 2015-10-25
        相关资源
        最近更新 更多