使用powershell在远程FTP上创建目录

时间:2011-04-21 04:01:41

标签: powershell ftp directory

我能够将文件放到具有修改版本...

的远程FTP上
          $File = "D:\Dev\somefilename.zip"
          $ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip"

           "ftp url: $ftp"

          $webclient = New-Object System.Net.WebClient
          $uri = New-Object System.Uri($ftp)

          "Uploading $File..."

          $webclient.UploadFile($uri, $File)

我遇到的问题是我试图将文件上传到不存在的目录,put失败。所以我需要先创建目标目录.GET-MEMBER似乎没有显示我可以调用以创建目录的任何方法,只显示文件操作。

1 个答案:

答案 0 :(得分:7)

我使用函数Create-FtpDirectory

function Create-FtpDirectory {
  param(
    [Parameter(Mandatory=$true)]
    [string]
    $sourceuri,
    [Parameter(Mandatory=$true)]
    [string]
    $username,
    [Parameter(Mandatory=$true)]
    [string]
    $password
  )
  if ($sourceUri -match '\\$|\\\w+$') { throw 'sourceuri should end with a file name' }
  $ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri);
  $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory
  $ftprequest.UseBinary = $true

  $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

  $response = $ftprequest.GetResponse();

  Write-Host Upload File Complete, status $response.StatusDescription

  $response.Close();
}

取自Ftp.psm1,您可以在其中找到FTP的其他功能。

致其他人:抱歉没有遵循众所周知的动词 - 名词模式。 ;)

相关问题