FTPS Powershell脚本

时间:2016-04-11 18:42:14

标签: powershell ftps

我想知道是否可以制作FTPS powershell脚本来上传和下载具有相同文件扩展名的所有文件?我看了,我总是看到ftp脚本。

我一直在使用这个脚本。

$sourceuri = ""
$targetpath = ""
$username = ""
$password = ""


$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)


$ftprequest.Credentials =
    New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $false
$ftprequest.KeepAlive = $false
$ftprequest.EnableSsl = $true


$ftpresponse = $ftprequest.GetResponse()

$responsestream = $ftpresponse.GetResponseStream()

$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024

do{
    $readlength = $responsestream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)

$targetfile.close()
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
At H:\-----.ps1:21 char:1
+ $ftpresponse = $ftprequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

You cannot call a method on a null-valued expression.
At H:\-------.ps1:24 char:1
+ $responsestream = $ftpresponse.GetResponseStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

这些是我得到的一些错误。

0 个答案:

没有答案