【问题标题】:Copy Files via FTP and using powershell通过 FTP 和使用 powershell 复制文件
【发布时间】:2015-10-28 05:16:15
【问题描述】:

我正在使用 jenkins 来实现 CI,并成功地将文件发布到构建服务器。 但是,部署到 App Server 并不起作用,因为 FTP 插件只是复制文件夹,而不是文件。 所以我正在编写一个powershell脚本来通过FTP复制文件。

到目前为止,我已经在谷歌搜索的帮助下写了这篇文章:

## Automate FTP uploads
## Go to destination
cd C:\TrashFolder
$location = Get-Location
"We are here: $location"
## Get files
$files = Get-ChildItem -recurse   
## Get ftp object
$ftp_client = New-Object System.Net.WebClient
$ftp_address = "ftp://MyUserName:MyPassword@ftp://IPAddress"
## Make uploads
foreach($file in $files)
  {

    $directory = "";
    $source = $file.DirectoryName + "\" + $file;
    if ($File.DirectoryName.Length -gt 0)
     {
        $directory = $file.DirectoryName.Replace($Location,"")
     }
   $Directory += "/";
   $FtpCommand = $ftp_address + $directory + $file

   $uri = New-Object System.Uri($FtpCommand)
   "Command is " + $uri + " file is $source"
    $ftp_client.UploadFile($uri, $source)
 }

但我得到了这个异常

Exception calling "UploadFile" with "2" argument(s): "The remote name could not be resolved: 'ftp'" At C:\DeployScripts\FileUpload.ps1:26 char:5 + $ftp_client.UploadFile($uri, $source) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException

我们将不胜感激。 TIA

【问题讨论】:

  • 地址解析出现错误。尝试 ping $ftp_address

标签: powershell jenkins ftp


【解决方案1】:

嗯,看起来 $ftp 地址字符串需要对初学者进行一些修复。

"ftp://MyUserName:MyPassword@server"

例子:

"ftp://Nick:MyPassword@myftpserver.net"

服务器 IP/名称前不应有第二个 ftp://。看看有没有帮助

【讨论】:

  • 我已经用建议的更正测试了你的代码,它现在对我有用,你能用你正在使用的更新代码修改你的问题吗?如果错误消息发生了变化。
  • Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request." At C:\DeployScripts\FileUpload.ps1:26 char:5 + $ftp_client.UploadFile($uri, $source) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
  • 我遇到的问题是用户名中有一个反斜杠“\”,我在 URI 解析时遇到错误。
  • 好的,这将是第 2 个问题,我用谷歌搜索了它。显然,您需要通过添加第二个反斜杠来转义用户名中的反斜杠,以便 \ 变为 \\ Try that
  • 我的用户名没有反斜杠。 URI 的值为Username:password@4NumberIPAddress/Views
猜你喜欢
  • 2010-12-24
  • 1970-01-01
  • 2014-07-16
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
相关资源
最近更新 更多