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似乎没有显示任何方法我可以调用创建目录,只进行文件操作.
我用的是功能 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的其他功能.
致其他人:抱歉不遵循众所周知的动词 - 名词模式.;)
归档时间: |
|
查看次数: |
6977 次 |
最近记录: |