This is a little scrappy, but it is a start...
I have extracted the Schedule ID from the [last_outcome_message] field, and joined it to the Schedule ID. That should give you the correct result set
I could not find anywhere else where the schedule_id is stored against a History
Event.
SELECT h.server
,j.name
,h.step_id
,h.step_name
,h.message
,h.run_status
,Rundatetime = CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)
,h.run_duration
,s.last_outcome_message
,sc.name AS ScheduleName
,s.last_run_time
,sjc.next_run_time
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
INNER JOIN msdb..sysjobservers s ON s.job_id=h.job_id
LEFT OUTER JOIN msdb..sysjobschedules sjc ON sjc.job_id=h.job_id
AND CONVERT(INT, LTRIM(RTRIM(REPLACE(LEFT(last_outcome_message,
PATINDEX('% (%', last_outcome_message)),
'The job failed. The Job was invoked by Schedule ', ''))))
= sjc.schedule_id
LEFT OUTER JOIN msdb..sysschedules sc ON sc.schedule_id=sjc.schedule_id
WHERE
run_status=0
AND step_name<>'(Job outcome)'
--Looking on failed job from the last 10 minutes.
AND CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121) > DATEADD(mi, -10, GETDATE())
ORDER BY h.run_date DESC ,h.run_time DESC
Xp_cmdshell would work fine - just access the file using the UNC path (\server1\share\file.exe). As the other person mentioned, make sure you have proper permissions for you sql agent service account to access the file.
If, instead, you actually want to run the file on the remote server, you can use a tool called PSEXEC that will run am exe on another server. This isn't common, but it sounds like it might be what you're looking for. You can get that tool here:
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Best Answer
The dbo.sysjobs and dbo.sysoperators views provide you the details. Some simple code (cleaned up from coding errors in first post.):
I did not flesh out the notify levels: 0 = none, 1 = succeeds, 2 = fails, 3 = completes. The delete_level is 0 if the job is not to automatically delete under any circumstance.