【问题标题】:PowerShell - How can I use webrequest class to upload to remote FTPs site?PowerShell - 如何使用 webrequest 类上传到远程 FTP 站点?
【发布时间】:2013-09-20 08:06:59
【问题描述】:

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

非常感谢任何帮助

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    这是一个快速示例。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2013-04-10
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多