Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Check SQL Error Logs using Powershell


Check SQL Error Logs using Powershell

Author
Message
jbreffni
jbreffni
SSChasing Mays
SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)

Group: General Forum Members
Points: 623 Visits: 266
Comments posted to this topic are about the item Check SQL Error Logs using Powershell



Brian Fountain
Brian Fountain
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 118
Hi,
I am receiving the following error when I run the script...

7/26/2013 12:51:31 PM - Checking Agent Error Logs on BOSSQL03...
Unable to read SQL Agent error log from server @{serverName=BOSSQL03}
jbreffni
jbreffni
SSChasing Mays
SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)

Group: General Forum Members
Points: 623 Visits: 266
Using SSMS, does this work:?


EXEC master..xp_readerrorlog



S. Kusen
S. Kusen
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1200 Visits: 1111
Thanks for sharing. That's a relatively quick and dirty way to get the error log entries from multiple systems. I don't yet have PS3 installed, so I modified your code to use sqlps and the invoke-sqlcmd cmdlet and it works as desired. Hope you don't mind me sharing that. If so, I can remove the post.


<#

_checkErrorLogs.ps1
Description:
Author: Jim Breffni.
This script displays the errorlog entries for all selected servers for the last n days.
The script can take a few minutes to run if the error logs are large and you are looking back over several days.

S.Kusen 2014-08-26: Modified to use SQLPS and invoke-sqlcmd for powershell v2 environment.

Requirements:
Module invokesqlquery needs to be installed.
1. Invokesqlquery can be downloaded from http://powershell4sql.codeplex.com/

#>

cls

import-module SQLPS

$today = (get-date).toString()
$all = @()
$lookback = ((get-date).adddays(-1)).ToString() # look back n days from current time

# Load up the list of servers to check
# Use this code if you can get your list of servers from a sql database
$servers = invoke-sqlcmd -Query @'
select distinct serverName from YOUR_TABLE
order by serverName
'@ -serverinstance YOUR_SERVER -database YOUR_DATABASE

# remove the comment from the code below if you want to supply the list of servers as an array
#$servers = 'sql01', 'sql02' | select-object @{Name = 'serverName'; Expression = {$_}}


foreach ($server in $servers) {

"$((get-date).toString()) - Checking SQL Error Logs on $($server.servername)..."

try {
$all += invoke-sqlcmd -query "EXEC master..xp_readerrorlog 0, 1, null, null, '$lookback', '$today' " -server $server.servername | select-object @{Name="Server"; Expression={$server.servername}}, LogDate, ProcessInfo, Text | `
where-object {$_.Text -match 'error|fail|warn|kill|dead|cannot|could|stop|terminate|bypass|roll|truncate|upgrade|victim|recover'} | `
where-object {$_.Text -notmatch 'setting database option recovery to'}
}
catch {"Unable to read SQL error log from server $server"}
} # foreach ($server in $servers) {

$all | out-gridview -title ("$($all.Count) errors in SQL Server Error Logs. From $lookback to $today")


$all = @()
foreach ($server in $servers) {
"$((get-date).toString()) - Checking Agent Error Logs on $($server.servername)..."
try {
$all += invoke-sqlcmd -query "EXEC master..xp_readerrorlog 0, 2, null, null, '$lookback', '$today' " -server $server.servername | select-object @{Name="Server"; Expression={$server.servername}}, LogDate, ProcessInfo, Text | `
where-object {[datetime]$_.LogDate -ge $lookback}
}
catch {"Unable to read SQL Agent error log from server $server"}
} # foreach ($server in $servers) {

$all | out-gridview -title ("$($all.Count) errors in SQL Agent Error Logs. From $lookback to $today")




jbreffni
jbreffni
SSChasing Mays
SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)

Group: General Forum Members
Points: 623 Visits: 266
nice, thank you!



tpo004
tpo004
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 20
09/01/2014 13:17:19 PM - Checking Agent Error Logs on SOME SERVERNAME...
Unable to read SQL Agent error log from server @{serverName=SOME SERVERNAME}

I'm experiencing the same problem with both methods: Dynamically or static



Any suggestions
jbreffni
jbreffni
SSChasing Mays
SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)SSChasing Mays (623 reputation)

Group: General Forum Members
Points: 623 Visits: 266
Without seeing your actual script the error could be caused by a permissions issue...



tpo004
tpo004
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 20
I'm sysadmin when running this and this is the script

<#

_checkErrorLogs.ps1
Description:
Author: Jim Breffni.
This script displays the errorlog entries for all selected servers for the last n days.
The script can take a few minutes to run if the error logs are large and you are looking back over several days.

Requirements:
Module invokesqlquery needs to be installed.
1. Invokesqlquery can be downloaded from http://powershell4sql.codeplex.com/

#>

cls

import-module invokesqlquery

$today = (get-date).toString()
$all = @()
$lookback = ((get-date).adddays(-1)).ToString() # look back n days from current time

# Load up the list of servers to check
# Use this code if you can get your list of servers from a sql database
$servers = invoke-sqlquery -query @'
select distinct MYCOLUMN from dbo.MYTABLE
order by MYCOLUMN
'@ -server MY SERVER -database MYDB

# remove the comment from the code below if you want to supply the list of servers as an array
#$servers = 'sql01', 'sql02' | select-object @{Name = 'serverName'; Expression = {$_}}


foreach ($server in $servers) {

"$((get-date).toString()) - Checking SQL Error Logs on $($server.servername)..."

try {
$all += invoke-sqlquery -query "EXEC master..xp_readerrorlog 0, 1, null, null, '$lookback', '$today' " -server $server.servername | select-object @{Name="Server"; Expression={$server.servername}}, LogDate, ProcessInfo, Text | `
where-object {$_.Text -match 'error|fail|warn|kill|dead|cannot|could|stop|terminate|bypass|roll|truncate|upgrade|victim|recover'} | `
where-object {$_.Text -notmatch 'setting database option recovery to'}
}
catch {"Unable to read SQL error log from server $server"}
} # foreach ($server in $servers) {

$all | out-gridview -title ("$($all.Count) errors in SQL Server Error Logs. From $lookback to $today")


$all = @()
foreach ($server in $servers) {
"$((get-date).toString()) - Checking Agent Error Logs on $($server.servername)..."
try {
$all += invoke-sqlquery -query "EXEC master..xp_readerrorlog 0, 2, null, null, '$lookback', '$today' " -server $server.servername | select-object @{Name="Server"; Expression={$server.servername}}, LogDate, ProcessInfo, Text | `
where-object {[datetime]$_.LogDate -ge $lookback}
}
catch {"Unable to read SQL Agent error log from server $server"}
} # foreach ($server in $servers) {

$all | out-gridview -title ("$($all.Count) errors in SQL Agent Error Logs. From $lookback to $today")

This is the result:

02-09-2014 08:14:52 - Checking SQL Error Logs on ...
Unable to read SQL error log from server @{instancename=SERVERNAME1\INSTANCE1 }
02-09-2014 08:15:07 - Checking SQL Error Logs on ...
Unable to read SQL error log from server @{instancename=SERVERNAME1\INSTANCE2 }
02-09-2014 08:15:07 - Checking SQL Error Logs on ...
Unable to read SQL error log from server @{instancename=SERVERNAME2\INSTANCE3 }
02-09-2014 08:15:07 - Checking Agent Error Logs on ...
Unable to read SQL Agent error log from server @{instancename=SERVERNAME1\INSTANCE1 }
02-09-2014 08:15:07 - Checking Agent Error Logs on ...
Unable to read SQL Agent error log from server @{instancename=SERVERNAME1\INSTANCE2 }
02-09-2014 08:15:07 - Checking Agent Error Logs on ...
Unable to read SQL Agent error log from server @{instancename=SERVERNAME2\INSTANCE3 }



I ran the script from ISE both as administrator and my account (which is sysadmin)
What permissions do I need other than that?
Thanks for your support
tpo004
tpo004
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 20
and EXEC master..xp_readerrorlog runs fine on all instances 2008r2, 2012sp1, 2014

I can see from the trace and the output that I'm able to retrieve the serverlist from the table so that part should be in order
Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10020 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search