2017-05-04 99 views
0

我有一個帶有服務器IP地址的文件。腳本通過地址來讀取地址($line),但有時會發生服務器地址下降。腳本運行到IP地址結束是必要的。所以我用-Erroraction ContinueSet_FTPConnection但反正腳本中斷。如何解決這個問題呢?通過FTP上傳文件的錯誤操作

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 
+0

您能否編輯您的問題以包含您收到的錯誤? –

回答

0

Continue實際上是defau lt設置。這意味着如果發生非終止錯誤,它將顯示錯誤,然後應繼續使用命令和腳本的其餘部分。你可以嘗試SilentlyContinue看看是否有幫助。

或者,您可以測試IP地址是否可以先連接。如果你使用的是Windows 8/Server 2012的或新的和PowerShell V4 +,你可以使用Test-NetConnection -Port 21爲FTP端口做到這一點而言,如果不是你可以使用Test-Connection而不是(這是一個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" 
    } 
} 
+0

而當我使用Win 7?我用ping替換了Test-NetConnection,問題是一樣的。 – TraPS

+0

你可以編輯你的問題與錯誤的詳細信息。 –

+0

修改我的回答,看看它是否有幫助。 –