我有一個通過PowerShell進行SQL Server數據庫備份的進程,它在遍歷數據庫時記錄到單個文本文件。當我們遇到錯誤時,我想將它們記錄到表中,但是本機T-SQL可以防止在Try-Catch塊中獲得備份錯誤消息,所以我想從文本文件中獲取這些信息。只解析文本文件的最後一部分
我想只抓取來自最新數據庫的消息。因此我的問題。
考慮下面的文本樣本:
12:31:32.310 #############
12:31:32.326 # Database1 #
12:31:32.326 #############
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Msg 5901, Level 16, State 1, Server Server1, Line 1
One or more recovery units belonging to database 'Database1' failed to generate a checkpoint. This is typically caused by lack of system resources such as disk or memory, or in some cases due to database corruption. Examine previous entries in the error log for more detailed information on this failure.
Msg 9002, Level 17, State 4, Server Server1, Line 1
The transaction log for database 'Database1' is full due to 'ACTIVE_TRANSACTION'.
Msg 3013, Level 16, State 1, Server Server1, Line 1
BACKUP DATABASE is terminating abnormally.
Msg 3201, Level 16, State 2, Server Server1, Line 4
Cannot open backup device 'D:\Backups\Database1\Database1_201408260031_backup_Native_DIFF.bak'. Operating system error 2(The system cannot find the file specified.).
Msg 3013, Level 16, State 1, Server Server1, Line 4
VERIFY DATABASE is terminating abnormally.
12:31:31.435 #############
12:31:31.435 # Database2 #
12:31:31.435 #############
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
BACKUP DATABASE [Database2] TO DISK = N'D:\Backups\Database2\Database2_backup_Native_FULL.bak' WITH STATS=10,INIT,COMPRESSION;
43 percent processed.
86 percent processed.
99 percent processed.
Processed 296 pages for database 'Database2', file 'Database2' on file 1.
100 percent processed.
Processed 2 pages for database 'Database2', file 'Database2_log' on file 1.
BACKUP DATABASE successfully processed 298 pages in 0.180 seconds (12.893 MB/sec).
The backup set on file 1 is valid.
12:31:32.310 #############
12:31:32.326 # Database3 #
12:31:32.326 #############
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Msg 5901, Level 16, State 1, Server Server1, Line 1
One or more recovery units belonging to database 'Database3' failed to generate a checkpoint. This is typically caused by lack of system resources such as disk or memory, or in some cases due to database corruption. Examine previous entries in the error log for more detailed information on this failure.
Msg 9002, Level 17, State 4, Server Server1, Line 1
The transaction log for database 'Database3' is full due to 'ACTIVE_TRANSACTION'.
Msg 3013, Level 16, State 1, Server Server1, Line 1
BACKUP DATABASE is terminating abnormally.
Msg 3201, Level 16, State 2, Server Server1, Line 4
Cannot open backup device 'D:\Backups\Database3\Database3_201408260031_backup_Native_DIFF.bak'. Operating system error 2(The system cannot find the file specified.).
Msg 3013, Level 16, State 1, Server Server1, Line 4
VERIFY DATABASE is terminating abnormally.
我只希望下面的輸出只是數據庫3:
One or more recovery units belonging to database 'Database3' failed to generate a checkpoint. This is typically caused by lack of system resources such as disk or memory, or in some cases due to database corruption. Examine previous entries in the error log for more detailed information on this failure.
The transaction log for database 'Database3' is full due to 'ACTIVE_TRANSACTION'.
BACKUP DATABASE is terminating abnormally.
Cannot open backup device 'D:\Backups\Database3\Database3_201408260031_backup_Native_DIFF.bak'. Operating system error 2(The system cannot find the file specified.).
VERIFY DATABASE is terminating abnormally.
我走了這麼遠,但需要上述篩選出任何錯誤數據庫3。
Get-Content ("C:\SampleError.txt") | Select-String -SimpleMatch "Msg" -Context 0, 1 | %{ ($_.Context.PostContext); }
我基本上需要過濾只有最後的塊(#數據庫3#)上獲取內容,但我無法找到這樣做的正確方法(LastIndexOf?)。我想避免出於可支持性的原因而使用正則表達式,但願意接受建議。謝謝你的幫助!
日誌文件有多大?將整個日誌讀入內存是否可行? – TheMadTechnician 2014-08-29 15:11:43
體積小,一般在1MB以下。我想記錄當我登錄到表時發生錯誤的時間,否則一旦完成所有備份,我就可以刪除文件。我需要知道錯誤X屬於數據庫Y,這在運行時更容易。 – FilamentUnities 2014-08-29 15:23:39