通过FTP和PowerShell复制文件

时间:2015-10-28 05:16:16

标签: powershell jenkins ftp

我正在使用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

1 个答案:

答案 0 :(得分:0)

好像看起来$ ftp地址字符串需要为初学者修复一些。

"ftp://MyUserName:MyPassword@server"

示例:

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

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