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

标签 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)

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

最佳答案

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

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

关于powershell - 使用 powershell 在远程 FTP 上创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5739366/

相关文章:

perl - 如何从特定目录中获取具有特定扩展名的所有文件的列表?

powershell - 从脚本中获取函数列表

powershell - 从批处理文件调用时,Send-MailMessage cmdlet捕获错误

docker - 如何在Docker中安装和配置ftp

java - 如何通过 SFTP 从服务器检索文件?

php - 如何在 PHP 中使用 FTP 从另一台服务器传输文件

php - 使用 php 创建 javascript 数组以从目录中获取图像

c++ - 如何将宏中#define 的值转换为 char* (c++)

function - 在 PowerShell 中查看嵌套私有(private)函数定义

powershell - 写入事件日志以查找错误