Saturday, April 28, 2012

Generate a list objects representing runs of a named task



Generate a list objects representing runs of a named task.  Each object will have Start, Stop, Elapsed and ReturnCode properties.  By default will look at the event log on the local computer but can optionally connect to any named computer.


This script can also serve as a decent example of how to work with the Windows Event Logs in general.





Powershell Code






<# 
.SYNOPSIS 
   Generate a list objects representing runs of the named task.  Each object  
   will have Start, Stop, Elapsed and ReturnCode properties.  If the task is 
   still running, Stopped, Elapsed and ReturnCode will be null. 
.PARAMETER TaskName 
   Name of task to report on 
.PARAMETER ComputerName 
   Computer to fetch events from 
.EXAMPLE 
   Get-TaskRuns.ps1 "\SomeJob" -ComputerName contososvr 
 
Started                       Stopped                       Elapsed                       ReturnCode 
-------                       -------                       -------                       ---------- 
2/21/2012 10:37:11 PM         2/21/2012 10:37:11 PM         00:00:00.3050305              0 
2/19/2012 4:42:20 AM          2/19/2012 4:42:20 AM          00:00:00.1730000              0 
2/19/2012 4:16:51 AM          2/19/2012 4:16:51 AM          00:00:00.5660000              0 
2/18/2012 10:39:18 PM         2/18/2012 10:39:18 PM         00:00:00.0380000              0 
.NOTES 
   File Name : Get-TaskRuns.ps1 
   Author    : Matthew King 
   Link        : http://gallery.technet.microsoft.com/scriptcenter/Generate-report-of-e35c27a8 
#>
 
 
param( 
  [Parameter(Mandatory=$true)] 
  [String]$TaskName, 
  [String]$ComputerName = (& hostname) 
) 
 
$startCode = 200 
$stopCode = 201 
$filter = @" 
 
  "0" Path="Microsoft-Windows-TaskScheduler/Operational"> 
    <Select Path="Microsoft-Windows-TaskScheduler/Operational">*[System[(EventID=$startCode or EventID=$stopCode)] and EventData[Data[@Name="TaskName"]="$TaskName"]]</Select> 
  </Query> 
</QueryList> 
"@ 
$events = get-winevent -Oldest -FilterXML $filter -ComputerName $ComputerName 
 
$instanceRegex = [regex]'{([^}]+)' 
$returnCodeRegex = [regex]'return code (\d+)' 
$runs = @{} 
$events | % { 
    $_.message -match $instanceRegex | Out-Null 
    $guid = $matches[1] 
    if (-not $runs.Contains($guid)) { 
        $run = New-Object System.Object 
        $run | Add-Member -type NoteProperty -name Started -value $null 
        $run | Add-Member -type NoteProperty -name Stopped -value $null 
        $run | Add-Member -type NoteProperty -name Elapsed -value $null 
        $run | Add-Member -type NoteProperty -name ReturnCode -value $null 
        $runs[$guid] = $run 
    } else { 
        $run = $runs[$guid]; 
    } 
    if ($_.id -eq $startCode) { 
        $run.Started = $_.timecreated; 
    } 
    elseif ($_.id -eq $stopCode) { 
        $run.Stopped = $_.timecreated; 
        $_.message -match $returnCodeRegex | Out-Null 
        $run.ReturnCode = $matches[1] 
        $run.elapsed = ($run.Stopped - $run.Started) 
    } 
} 
 
$runs.values | Sort Started -Descending

Source:  Technet Contributor



Location:Earl Ave,Yukon,United States

No comments: