使用PsFtp将文件上传到FTP Powershell

时间:2014-04-03 09:14:11

标签: powershell upload ftp

我在这个问题上一直在抨击我的大脑,似乎无法修复它。我正在尝试使用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。我也尝试删除变量并使用硬编码路径;我得到了同样的错误。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

使用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路径。如果您不想使用相同的名称,则必须先在本地重命名文件,或者在之后远程重命名。

答案 2 :(得分:-1)

更改块

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

到一行

Get-ChildItem -Path $local_out |  Add-FTPItem  -Path $ftp_path
相关问题