0

我想使用 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 的信息,但不知道如何使用。

非常感谢任何帮助

4

1 回答 1

0

PowerShell Pipeworks中有一个函数,一个 PowerShell 模块和在 PowerShell 上编写的 Web 语言,允许您将数据上传到 FTP。它被称为 Push-FTP。

这是一个快速示例。

Push-Ftp -Path c:\Example -Include *.aspx

它是使用 Net.Webclient 构建的,所以如果你不喜欢它,你可以打开它看看它是如何工作的。

于 2013-09-19T23:20:00.413 回答