使用 powershell 在远程 FTP 上创建目录 [英] Creating a directory on remote FTP using powershell

查看:28
本文介绍了使用 powershell 在远程 FTP 上创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将文件上传到远程 FTP 并使用修改后的版本...

I"m able to put a file up to a remote FTP with a modified version of...

          $File = "D:Devsomefilename.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 似乎没有显示我可以调用的任何方法来创建目录,只有文件操作.

I'm running into the problem that I"m trying to upload a file to a directory that doesn't exist, the put fails. So I need to create the target directory first. GET-MEMBER doesn't seem to show any methods I can invoke to create a directory, only file operations.

推荐答案

我用函数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.

Taken from Ftp.psm1 where you can find also other functions for FTP.

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

这篇关于使用 powershell 在远程 FTP 上创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆