通过FTP上传文件时出错

时间:2017-05-04 09:59:20

标签: powershell error-handling ftp

我有一个服务器IP地址的文件。并且脚本通过地址读取地址($line),但有时会发生将要关闭的服务器的地址。有必要编写钢铁运行直到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

使用If:

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

1 个答案:

答案 0 :(得分:0)

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

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

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"
    }
}