Searching the SQL Error Log with PowerShell

#############################################################################    ################
#
# NAME: Search-SQLErrorLog.ps1
# AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com
# DATE:22/07/2013
#
# COMMENTS: Load function for Searching SQL Error Log and exporting and     displaying to CSV
# ————————————————————————

Function Search-SQLErrorLog ([string] $SearchTerm , [string] $SQLServer) {
 
    $FileName = 'c:\TEMP\SQLLogSearch.csv'
    $Search = '*' + $SearchTerm + '*'
    $server = new-object "Microsoft.SqlServer.Management.Smo.Server"     $SQLServer
    $server.ReadErrorLog(5)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |Export-Csv $FileName
    $server.ReadErrorLog(4)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |ConvertTo-Csv |Out-File $FileName -append
    $server.ReadErrorLog(3)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |ConvertTo-Csv |Out-File $FileName -append
    $server.ReadErrorLog(2)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |ConvertTo-Csv |Out-File $FileName -append
    $server.ReadErrorLog(1)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |ConvertTo-Csv |Out-File $FileName -append
    $server.ReadErrorLog(0)| Where-Object {$_.Text -like $Search} | Select     LogDate, ProcessInfo, Text |ConvertTo-Csv |Out-File $FileName -append
    Invoke-Item $filename
}
Built with Hugo
Theme Stack designed by Jimmy