将文件上载到FTP服务器时出错

时间:2015-10-07 08:59:53

标签: powershell ftp powershell-v2.0

我尝试使用FTP将文件上传到FTP服务器。我在网上找到了以下脚本,但我无法使用它。

$UserName = 'username'

$Password = 'password'

$LocalFilePath = 'c:\FolderName\x.txt'

$RemoteFileName = 'x.txt'

$ServerName = 'my.ftpserver.co.uk'


$webclient = New-Object System.Net.WebClient

$webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)

#Connect to FTP
$uri = New-Object System.Uri(“ftp://$ServerName/$RemoteFileName”)
write-host $uri

#upload as file
$webclient.UploadFile($uri, $LocalFilePath)

但是当我运行这个时,我收到以下错误:

    Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At line:21 char:22
+ $webclient.UploadFile <<<< ($uri, $LocalFilePath)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

有人能指出我正确的方向吗? 我可以从我的PC上使用Filezilla等进行连接,因此它不会被防火墙或任何东西阻挡,

2 个答案:

答案 0 :(得分:0)

测试了你的脚本并且运行正常,只有我能够重现你的错误才能将$ LocalFilePath指向一个不存在的文件。你能尝试一下:

Test-Path($LocalFilePath)

看看它是否返回True?

答案 1 :(得分:0)

从你的评论和我在问题中看到的代码中,问题可能只是你有聪明的引语。它将是您的编码编辑器的产品,或者是将代码复制并粘贴到您的环境中的源代码。你需要注意这些事情。假设路径正确形成或许这只是你的问题。

智能引号

$uri = New-Object System.Uri(“ftp://$ServerName/$RemoteFileName”)

适当的双引号

$uri = New-Object System.Uri("ftp://$ServerName/$RemoteFileName")

第二个示例中的引号是您应该使用的引号。

相关问题