【问题标题】:Create output script file of FTP upload创建 FTP 上传的输出脚本文件
【发布时间】:2017-10-01 11:52:22
【问题描述】:

我有一个将文件上传到 FTP 服务器的脚本,我有一个服务器列表 IP $FTPServer 和文件目录上传 \Upload。它使用PSFTP module

这是我读取文件$item并上传到IP$line的代码:

foreach ($line in $FTPServer)
{
    foreach ($item in (Get-ChildItem .\Upload))
    {
        Start-Transcript -Path $results
        Write-Host -Object "ftp url: $line" 
        Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive 
        $Session = Get-FTPConnection -Session MySession 
        Write-Host -Object "Uploading $item..."
        Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Confirm:$false
        Stop-Transcript
    }
}

问题是,有时并非所有文件都可以上传,或者某个服务器已关闭。因此需要保存上传的结果。我想到了类似 table 的东西,它将存储在 logs.txt 中,看起来像这样:

FTP 服务器文件已连接上传 ---------- --------- -------- -------- 192.168.1.2 test.txt OK OK #当连接上传文件时 192.168.1.2 conf.dll OK FAIL #当连接但没有上传 192.168.1.20 test.txt FAIL FAIL #当没有连接和没有上传

我在考虑哈希表:

$array = $line, $item, $array[2], $array[3]
$FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
$FailTable|Out-File '.\stats.txt'

但我不知道如何将连接状态和上传到$array[2]$array[3] 我会很高兴有任何帮助。

【问题讨论】:

    标签: powershell ftp


    【解决方案1】:

    您可以将数组构建为服务器和文件名的组合,然后存储结果passfail

    所以,你有表:

    192.168.1.2 test.txt OK OK #当连接上传文件时 192.168.1.2 conf.dll OK FAIL #当连接但没有上传 192.168.1.20 test.txt FAIL FAIL #当没有连接和没有上传

    看起来像:

    "192.168.1.2\test.txt","OK" "192.168.1.2\conf.dll","OK" "192.168.1.20\test.txt","失败"

    如果您在 IP 和名称之间使用了不允许的字符(例如 *,则可以使用 split("*") 将它们分开。

    根据您用于 FTP 的模块,您可能会说:

    $results = Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Confirm:$false
    

    然后查看$results的值,看看是否有问题(也许?)

    【讨论】:

      【解决方案2】:

      我昨天为自己的问题添加了答案:

      我已经制作了一个 ftp 脚本文件,带有 ftp.ini 文件。
      该文件位于 c:\hotbox\ftpbat.bat

      我在错误的目录中。该程序运行良好,但我在不同的驱动器索引中运行 bat 程序。我在我的 VB6-index 中测试它

      对不起。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-18
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 2019-10-07
        • 1970-01-01
        相关资源
        最近更新 更多