Here is a script that I had that reports on a specific task or all tasks for a given number of hours. You can modify it to write to SQL.
#----------------------------------------------
# Script: TaskResults.ps1
# Author: Motox80 on Microsoft Technet Forums
#----------------------------------------------
param( $TaskName = '',
$Hours=1,
[switch]$debug) # The task name to monitor
#start-transcript -path c:\temp\tr.log
cls
"Analyzing task results for {0} for a period of {1} hours. " -f $taskname, $Hours # Time frame in hours
$tf = $Hours * 3600000
$elna = @('Microsoft-Windows-TaskScheduler/Operational')
#$AllEvents = $null
if ($AllEvents -eq $null) {
'Reading events.'
$AllEvents = @() # prepare array so we can append to it
$xml = "<QueryList><Query Id=""0"" Path=""Microsoft-Windows-TaskScheduler/Operational"">
<Select Path=""Microsoft-Windows-TaskScheduler/Operational"">*[System[TimeCreated[timediff(@SystemTime) <= $tf]]]</Select>
</Query></QueryList>"
$AllEvents = Get-WinEvent -FilterXml $XML -ErrorAction SilentlyContinue # append the events (if any)
}
else {
'Reusing event data.' # when testing with ISE
}
[System.Collections.ArrayList]$AllTasks =@()
foreach ($event in $Allevents){
[xml]$xe = $event.ToXml()
$TName = (($xe.Event.EventData.Data | Where-Object name -eq TaskName).'#text') + ""
$IID = ($xe.Event.EventData.Data | Where-Object name -eq InstanceId).'#text' + ($xe.Event.EventData.Data | Where-Object name -eq TaskInstanceId).'#text'
if ($debug) {
"{0} - {1} - {2}" -f $event.Id, $TName, $IID
}
if(($TName -match $Taskname) -or ($TaskName -eq "")) {
if ($IID) {
$cid = $Alltasks | where {$_.ID -eq $IID}
if ($cid -eq $null) { #add a new entry
$cid = [PSCustomObject]@{
ID = $IID
Name = $TName
Start = $event.TimeCreated # default to the event time
End = $null
Trigger = $null
RC = $null
}
$foo = $Alltasks.add($cid)
}
if ($event.Id -eq 100) {
$cid.Start = $event.TimeCreated
}
if ($event.Id -eq 102) {
$cid.End = $event.TimeCreated
}
if ($event.Id -eq 107) {
$cid.Trigger = 'Time'
}
if ($event.Id -eq 110) {
$cid.Start = $event.TimeCreated
}
if ($event.Id -eq 200) {
$cid.Name = $TName
}
if ($event.Id -eq 201) {
[long]$rc = ($xe.Event.EventData.Data | Where-Object Name -eq ResultCode).'#text'
if ($rc -gt 2147483648) { # greater than 8000 0000
$cid.rc = "{0:X}" -f $rc # format as hex
} else {
$cid.rc = $rc # else use decimal
}
}
if ($event.Id -eq 203) {
$cid.Trigger = "Failed"
[long]$rc = ($xe.Event.EventData.Data | Where-Object Name -eq ResultCode).'#text'
if ($rc -gt 2147483648) { # greater than 8000 0000
$cid.rc = "{0:X}" -f $rc # format as hex
} else {
$cid.rc = $rc # else use decimal
}
}
#$lastevent = $event
} else {
# "{0} {1} {2}" -f $event.Id, $event.TimeCreated, ($event.Message + ( " " * 160)).substring(0,160).trim()
}
}
}
# Now output the reults
if ($debug) {
$AllTasks | Format-Table -AutoSize
"==============================================================="
}
$AllTasks | Format-Table -AutoSize -Property Start, End, RC, Trigger, Name
#stop-transcript