【问题标题】:Creating a directory on remote FTP using powershell使用 powershell 在远程 FTP 上创建目录
【发布时间】:2011-08-10 00:12:54
【问题描述】:

我可以将文件上传到远程 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 似乎没有显示我可以调用的任何方法来创建目录,只有文件操作。

【问题讨论】:

    标签: powershell ftp directory


    【解决方案1】:

    我使用函数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 的其他功能。

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

    【讨论】:

    • 你为什么要调用throw 来获取正则表达式?为什么我们在创建目录的时候sourceuri需要是一个文件?
    • @JohnBubriski 这是您正在创建的目录的“文件名”。这基本上意味着URI应该像ftp://.../directory而不是ftp://.../directory/
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    相关资源
    最近更新 更多