【问题标题】:Erroraaction in uploading file by FTP通过 FTP 上传文件时出错
【发布时间】:2017-05-04 09:59:20
【问题描述】:

我有一个包含服务器 IP 地址的文件。并且脚本通过地址读取地址($line),但有时可能会出现服务器地址将关闭。有必要编写 steel run 脚本直到 IP 地址结束。所以我用-Erroraction ContinueSet_FTPConnection 但是脚本还是中断了。如何解决这个问题?

foreach ($line in $FTPServer)
    {
        Start-Transcript -Path $results            
        Write-Host -Object "ftp url: $line" 
        Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive -ErrorAction Continue
        $Session = Get-FTPConnection -Session MySession
        $Session>>.\sessions.txt 
        #Write-Host $Error[0]
        if($session.UsePassive -eq "True"){$connect="OK"}
        else{$connect="FAIL"}

        foreach ($item in (Get-ChildItem .\Upload))
        {
            #Get-FTPChildItem -Session $Session -Path /htdocs #-Recurse
            Write-Host -Object "Uploading $item..."
            $Send= Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Overwrite -ErrorAction Continue #>> .\up.txt #.\Upload\test.txt
            $item|gm >>.\up.txt
            if($Send.Name -eq $item.Name){$Rec="OK"}
            else{$Rec="!!!-FAIL-!!!"}
            $array = $line, $item, $connect, $Rec
            $FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
            Add-Content .\stats.txt $FailTable
        }
        Stop-Transcript
    }

错误: 来自我的代码

Transcript started, output file is .\logs.txt
ftp url: 10.80.59.173
Set-FTPConnection : Exception calling "GetResponse" with "0" argument(s): "Unable to connect to the remote server"
At F:\DPI FTP\FTPUpload_v2.ps1:25 char:13
+             Set-FTPConnection -Credentials $FTPCredential -Server $li ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Set-FTPConnection

Test-NetConnection

Start-Transcript : Transcription cannot be started.
At F:\DPI FTP\FTPUpload_v2.ps1:21 char:9
+         Start-Transcript -Path $results            #if $session.usepa ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand

ftp url: 10.80.59.173
Test-NetConnection : The term 'Test-NetConnection' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch
eck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At F:\DPI FTP\FTPUpload_v2.ps1:23 char:13
+         If (Test-NetConnection $line -Port '21')
+             ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Test-NetConnection:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Transcript started, output file is .\logs.txt
ftp url: 10.80.59.170
Test-NetConnection : The term 'Test-NetConnection' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch
eck the spelling of the name, or if a path was included, verify that the path is correct and try again.
At F:\DPI FTP\FTPUpload_v2.ps1:23 char:13
+         If (Test-NetConnection $line -Port '21')
+             ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Test-NetConnection:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果:

WARNING: Could not connect to 10.80.59.173
Start-Transcript : Transcription cannot be started.
At F:\DPI FTP\FTPUpload_v2.ps1:20 char:9
+         Start-Transcript -Path $results            #if $session.usepa ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand


Stop-Transcript : An error occurred stopping transcription: The host is not currently transcribing.
At F:\DPI FTP\FTPUpload_v2.ps1:47 char:9
+         Stop-Transcript
+         ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Stop-Transcript], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.StopTranscriptCommand

【问题讨论】:

  • 能否编辑您的问题以包含您收到的错误?

标签: powershell error-handling ftp


【解决方案1】:

Continue 实际上是默认设置。这意味着如果发生非终止错误,它将显示错误,然后应该继续执行命令和脚本的其余部分。你可以试试SilentlyContinue 看看是否有帮助。

或者,您可以先测试 IP 地址是否可连接。如果您使用的是 Windows 8/Server 2012 或更新版本以及 PowerShell v4+,您可以使用 Test-NetConnection -Port 21 专门针对 FTP 端口执行此操作,否则您可以使用 Test-Connection 代替(这是 Ping 的 PS 等效项):

foreach ($line in $FTPServer)
{
    Start-Transcript -Path $results            
    Write-Host -Object "ftp url: $line" 

    If (Test-Connection $line) {

        Set-FTPConnection -Credentials $FTPCredential -Server $line -Session MySession -UsePassive -ErrorAction Continue
        $Session = Get-FTPConnection -Session MySession
        $Session>>.\sessions.txt 
        #Write-Host $Error[0]
        if($session.UsePassive -eq "True"){$connect="OK"}
        else{$connect="FAIL"}

        foreach ($item in (Get-ChildItem .\Upload))
        {
            #Get-FTPChildItem -Session $Session -Path /htdocs #-Recurse
            Write-Host -Object "Uploading $item..."
            $Send= Add-FTPItem -Session $Session -Path $FTPPlace -LocalPath .\Upload\$item -Overwrite -ErrorAction Continue #>> .\up.txt #.\Upload\test.txt
            $item|gm >>.\up.txt
            if($Send.Name -eq $item.Name){$Rec="OK"}
            else{$Rec="!!!-FAIL-!!!"}
            $array = $line, $item, $connect, $Rec
            $FailTable=New-Object -TypeName PSObject -Property ([ordered]@{"FTP Server"=$array[0]; "File"=$array[1];"Connected"=$array[2];"Uploaded"=$array[3]})
            Add-Content .\stats.txt $FailTable
        }
        Stop-Transcript

    } Else {
        Write-Warning "Could not connect to $line"
    }
}

【讨论】:

  • 当我使用 Win 7 时?我用 ping 替换了 Test-NetConnection,问题是一样的。
  • 你能用错误的详细信息编辑你的问题吗?
  • 修改了我的答案,看看是否有帮助。
  • 它有效,但我有关于成绩单的错误(查看问题),我需要在 $FailTable 中保存状态“FAIL” - 当我在哈希表中使用 If 条件时,只会使用“OK”的服务器。我需要有关不可用服务器的存储信息。
  • 看起来Start-Transcript 在覆盖之前的脚本文件时遇到了问题。一个快速的谷歌建议如果运行脚本的用户可以写入文件但不能修改它们(根据他们的 ACL),就会发生这种情况。尝试将脚本文件写入其他路径,或手动删除前一个路径,或检查您的权限。
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 2012-04-26
  • 2013-03-24
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多