2017-06-15 51 views
1

我已經做了幾個小時的研究,目前卡住了,我看了一堆文件,然後將它傳遞給一些書面函數,我遇到的問題是如果我有200個文件要處理,我不希望每個錯誤都終止腳本,因爲這意味着整個事情需要重新執行。Powershell腳本將不會繼續,即使出現錯誤

所以我想使用Try/Catch或其他任何方式來捕獲錯誤,以便我知道它,但我希望循環移動到下一個項目並處理它。當我在循環中刪除Try..Catch並指定erroraction ='continue'時,它確實繼續,但是因爲數據庫連接仍處於打開狀態,所以所有文件都失敗了。

這裏的任何想法?

因此,目標是在循環過程中,如果遇到文件錯誤,請繼續前進到下一個文件,但突出顯示錯誤。

function GetDatabaseFiles ([STRING]$backupfile) 
 
{ 
 

 
Try 
 
{ 
 

 
$SQLConnection.Open() 
 
$SQLQuery = "RESTORE FILELISTONLY 
 
FROM DISK = N'$backupfile' 
 
WITH NOUNLOAD" 
 
$SQLCommand = New-Object system.Data.SqlClient.SqlCommand 
 
$SQLCommand.CommandText = $SQLQuery 
 
$SQLCommand.Connection = $SQLConnection 
 
$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
 
$SQLAdapter.SelectCommand = $SQLCommand 
 
$DataSet = New-Object System.Data.DataSet 
 
$SqlAdapter.Fill($DataSet) 
 
$SQLConnection.Close() 
 
return $DataSet.Tables[0] | Select-Object LogicalName,PhysicalName,type 
 
} 
 

 

 
Catch 
 
{ 
 
# Handle the error 
 
$err = $_.Exception 
 
write-host $err.Message -ForegroundColor Red 
 
while($err.InnerException) { 
 
$err = $err.InnerException 
 
write-host $err.Message -ForegroundColor Red 
 
LogInfo -db "Database file - $backupfile" -message "ERROR DETAILS for Getting DB Files section !!!! $err.Message" 
 

 
} 
 
if ($error) { $failedcount ++ } \t 
 
} 
 

 

 

 
} 
 

 

 

 

 
[STRING]$SQLServer    = $dbserver 
 
[STRING]$SQLDatabase    = 'master' 
 
[STRING]$SQLConnectString  = "Data Source=$SQLServer; Initial Catalog=$SQLDatabase; Integrated Security=True; Connection Timeout=0" 
 
[OBJECT]$SQLConnection    = New-Object System.Data.SqlClient.SqlConnection($SQLConnectString); 
 

 
$files = Get-ChildItem $backup_path -Recurse | Where-Object {$_.extension -eq ".bak"} | Sort-Object $_.name 
 
$total_count = $files.Length 
 

 
Try 
 
{ 
 

 
$error.clear() 
 
# Start looping through each backup file and restoring the databases 
 
foreach ($filename in $files) { 
 

 
$filecount ++ 
 
write-host "Currently attemping to restore the backup file $filename number $filecount" -ForegroundColor "Green" 
 

 
#Set the filename variable to the fullpath/name of the backup file 
 
$filename = $filename.FullName 
 

 

 
$dbFiles = GetDatabaseFiles -backupfile $filename #-ErrorAction Continue 
 
$dbFiles = $dbFiles[1..$dbFiles.Length] 
 

 

 
} 
 
} 
 

 

 
catch 
 
{ 
 
# Handle the error 
 
$err = $_.Exception 
 
write-output $err.Message 
 
while($err.InnerException) { 
 
$err = $err.InnerException 
 
write-output $err.Message 
 

 

 
} 
 

 
if ($error) { $failedcount ++ } \t 
 
} 
 
finally { 
 
write-output "script completed" 
 

 
}

回答

4

你的代碼是報價冗長。我在你的循環中看到這個結構:

Try{ 

    Foreach(...){ 
     $dbFiles = GetDatabaseFiles -backupfile $filename #-ErrorAction Continue 
    } 

} 
catch{ 
    ... 
} 
finally{ 
    write-output "script completed" 
} 

而是試試這個。 -ErrorAction Stop會將非終止錯誤轉變爲終止錯誤。 Try/catch不適用於非終止錯誤。

Foreach(...){ 

    Try{ 
     $dbFiles = GetDatabaseFiles -backupfile $filename #-ErrorAction Stop 
    } 
    catch{ 

    } 

} 
write-output "script completed"