Author Posts

April 13, 2016 at 2:44 am

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

April 13, 2016 at 2:49 am

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

April 13, 2016 at 4:07 am

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).

April 13, 2016 at 7:03 am

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

April 14, 2016 at 1:40 am

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