我想使用 WebRequest 类上传到远程安全 ftp 站点,并尝试使用 Webclient 但遇到各种异常。
可以说我已经成功地使用 Filezilla 连接并上传远程文件,但想使用 powershell 来自动化这个过程。
Client server has windows 2008 R2 standard - powershell version is 2.0
这是我运行的代码,下面是我现在得到的异常
#setup permission + working dir
$user = "ftpclient"
$password = "****"
$localdir = "C:/Extracts/foldern/"
$ftpdir = "ftps://100.100.100.0:900/Outbound"
###########################
$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential($user,$password)
foreach($item in Get-ChildItem -recurse $localdir)
{
$filename = [system.io.path]::GetFullPath($item.FullName).SubString([system.io.path]::GetFullPath($localdir).Length + 1)
Write-Host "Send $item..."
$file = New-Object System.Uri($ftpdir+"/"+$filename)
$client.UploadFile($file, $item.FullName)
}
Transfert de EDI_CIG_OrbitExtract.zip...
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:5 char:31
+ $client.UploadFile <<<< ($file, $item.FullName)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我在某处读过有关使用 Webrequest 而不是 Webclient 的信息,但不知道如何使用。
非常感谢任何帮助