powershell - FTP上传文件时出错

标签 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

最佳答案

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

关于powershell - FTP上传文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779691/

相关文章:

windows - 将文件从远程 RDP 复制到本地计算机

Angular:仅在为产品构建时获取模块未找到错误

Java:针对多错误功能的正确设计

haskell - 如何在Haskell中捕获错误并忽略它?

php - 错误的 PHP 代码可能导致此问题(错误 :Connection timed out) type of errors in FTP?

linux - 从 linux shell 中的 'ftp' 命令获取退出状态代码

powershell - Azure 帐户级 SAS token : Possible to Have a Policy?

powershell - 为什么单元素哈希表在 powershell 中的行为不同?

powershell - 测试连接内解析 DnsName

java - 无法使用 Apache Commons FTPClient 访问 FTP 服务器上的子文件夹