我寫了一個powershell shell腳本來監視sql server errorlog並報告特定的錯誤。我存儲了上次停止閱讀的位置,然後在下次運行腳本時繼續。這是實際讀取日誌的部分。然後,您只需將該位置存儲在某個臨時文件中,並將其作爲計劃任務運行即可。如果發生錯誤,甚至重新啓動某項服務,請發送電子郵件。
$path = $logs.file
Write-Host $path
if($currentLog.lastpos -ne $null){$pos = $currentLog.lastpos}
else{$pos = 0}
if($logs.enc -eq $null){$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite))}
else{
$encoding = $logs.enc.toUpper().Replace('-','')
if($encoding -eq 'UTF16'){$encoding = 'Unicode'}
$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite), [System.Text.Encoding]::$encoding)
}
$required = $br.BaseStream.Length - $pos
if($required -lt 0){
$pos = 0
$required = $br.BaseStream.Length
}
if($required -eq 0){$br.close(); return $null}
$br.BaseStream.Seek($pos, [System.IO.SeekOrigin]::Begin)|Out-Null
$bytes = $br.ReadBytes($required)
$result = [System.Text.Encoding]::Unicode.GetString($bytes)
$split = $result.Split("`n")
foreach($s in $split)
{
if($s.contains(" Error:"))
{
#Filter events here
}
}
$currentLog.lastpos = $br.BaseStream.Position
$br.close()
不錯!感謝分享,我會盡快嘗試。我認爲Windows Server 2003將需要安裝Powershell,因爲它不會預安裝,與Windows Server 2008不同。 – fuzzyanalysis 2012-07-14 02:50:04
我也希望在Windows 2003 Server上使用一些內置解決方案,因爲服務器不是我的,我也是出於安全原因,不允許將新軟件導入服務器。 – fuzzyanalysis 2012-07-18 00:31:11
然後,您可以編寫一個小的可視化基本腳本來執行此操作,並將其作爲計劃任務執行。然後,您需要導入的是您的腳本。我不知道窗口中的任何內置功能會爲您執行此操作。 – Gisli 2012-07-18 07:58:32