Wednesday, September 17, 2014

CMS: List Jobs not Writing to the Windows Event Log

Using CMS, we can get a list of all the SQL Agent Jobs that aren't writing to the Windows Event Log upon failure, and set them to do so.

-- Check for Jobs that aren't writing to the Event Log. 
declare @JobName sysname

-- 0 - Never 
-- 1 - On success 
-- 2 - On failure 
-- 3 - Always 
declare @NotifyLevelEventLog int
set @NotifyLevelEventLog = 2

declare cur cursor fast_forward for
    select name
      from msdb..sysjobs
     where enabled = 1
       and notify_level_eventlog = 0
       and name <> 'syspolicy_purge_history'
       and name <> 'MySpecialJob'
       and @@servername not in ('MYSERVER1', 'MYSERVER2')
  order by name

open cur
fetch next from cur into @JobName

while @@fetch_status = 0
begin
    print 'Setting notify_level_eventlog on server ' + @@servername + ', Job "' + @JobName + 
          '" to ' + cast(@NotifyLevelEventLog as nvarchar(50))
    exec msdb..sp_update_job @job_name = @JobName, @notify_level_eventlog = @NotifyLevelEventLog
    fetch next from cur into @JobName
end

close cur
deallocate cur

0 comments:

Post a Comment