-1

How can I get the taskscheduler's level in history using PowerShell?

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What have you tried mate? You can use `$service = new-object -ComObject("Schedule.Service") ; $service.Connect() ; $rootFolder = $service.GetFolder("\") ;` – Ranadip Dutta Nov 27 '17 at 11:46

1 Answers1

4

Use:

$EventFilter = @{
    LogName = 'Microsoft-Windows-TaskScheduler/Operational'
    Id = 100
    StartTime = [datetime]::Now.AddDays(-1)
}
# PropertySelector for the Correlation id (the InstanceId) and task name
[string[]]$PropertyQueries = @(
    'Event/EventData/Data[@Name="InstanceId"]'
    'Event/EventData/Data[@Name="TaskName"]'
)
$PropertySelector = New-Object System.Diagnostics.Eventing.Reader.EventLogPropertySelector @(,$PropertyQueries)

# Loop through the start events
$TaskInvocations = foreach($StartEvent in Get-WinEvent -FilterHashtable $EventFilter){

    # Grab the InstanceId and Task Name from the start event
    $InstanceId,$TaskName = $StartEvent.GetPropertyValues($PropertySelector)

    # Create custom object with the name and start event, query end event by InstanceId
    [pscustomobject]@{
        TaskName = $TaskName
        StartTime = $StartEvent.TimeCreated
        EndTime = $(Get-WinEvent -FilterXPath "*[System[(EventID=102)] and EventData[Data[@Name=""InstanceId""] and Data=""{$InstanceId}""]]" -LogName 'Microsoft-Windows-TaskScheduler/Operational' -ErrorAction SilentlyContinue).TimeCreated
    }
}
$TaskInvocations

Reference link: TaskScheduler History

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • That is working sir.... can you also help me getting the EVENT ID of a task form its task history. – bhargav porapu Nov 27 '17 at 12:45
  • @bhargavporapu: You need to try on your own about all these. SO is not a script delivery service, its more of a help forum where ppl get stuck and try figure out the direction. I am not seeing any effort from your side . – Ranadip Dutta Nov 27 '17 at 13:26
  • i just wanna know instated of giving the Event Id as a hard coded value , I want to get it if we provide a particular task's name. ok nothing to worry i got the solution, thq for the help so far. – bhargav porapu Nov 28 '17 at 04:40
  • happy to help. Enjoy Coding :) @bhargavporapu – Ranadip Dutta Jan 24 '19 at 06:45