Get Scheduled task statistics

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Mark Prior Mark Prior 7 months, 3 weeks ago.

  • Author
    Posts
  • #37636
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Hi can do most of what i want here, however im struggling to get the "Run Start" and "Run End" parameters to show through powershell, just lack of knowledge i guess.

    Get-ScheduledTask | Get-ScheduledTaskInfo

    gives me the below

    LastRunTime : 07/04/2016 10:54:54
    LastTaskResult : 267009
    NextRunTime :
    NumberOfMissedRuns : 0
    TaskName : CacheTask
    TaskPath : \Microsoft\Windows\Wininet\
    PSComputerName :

    but i would like to see the "Run Start" and "Run End" parameters like you do when you open up the Task scheduler GUID

    Edit actually Last run time would be "Run Start" so Just "Run End" needed really

    Thanks

  • #37637
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    That information doesn't appear to be available on the CIM class called by the cmdlet

  • #37640
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you're referring to the information in the History tab of the Task Scheduler GUI, it's getting that stuff from the event log (specifically: Microsoft-Windows-TaskScheduler/Operational).

  • #37664
    Profile photo of Mark Prior
    Mark Prior
    Participant

    thanks Richard, just needed someone else to sanity check for me

    Dave thats perfect, i should be able to get what i need from there, thanks

  • #37724
    Profile photo of Mark Prior
    Mark Prior
    Participant

    just an update to this guys i found this script below, couldnt really get it working properly though, i can see the events start & stop with the relevant 200/201 codes but script just refuses to pick this up, tried on v2 & v4 PS

    outputted:

     .\Get-taskruns.ps1 -TaskName "\EV-Clear-State" -computername PCNAME
    
    Started                       Stopped                       Elapsed                       ReturnCode
    -------                       -------                       -------                       ----------
    09/01/2016 06:45:00
    
    
     
     
    param( 
      [Parameter(Mandatory=$true)] 
      [String]$TaskName, 
      [String]$ComputerName = (& hostname) 
    ) 
     
    $startCode = 200 
    $stopCode = 201 
    $filter = @" 
     
       
        *[System[(EventID=$startCode or EventID=$stopCode)] and EventData[Data[@Name="TaskName"]="$TaskName"]] 
       
     
    "@ 
    $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

You must be logged in to reply to this topic.