我可以将文件上传到远程 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/