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

rya*_*yan 5 directory ftp powershell

我能够将文件放到具有修改版本的远程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)
Run Code Online (Sandbox Code Playgroud)

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

ste*_*tej 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();
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 为什么你要为正则表达式调用`throw`?为什么在我们创建目录时sourceuri需要是一个文件? (3认同)