任务计划程序日志,以下事件通道:
Microsoft-Windows-TaskScheduler/Operational
您可以使用Get-WinEvent
来收集均匀TS。通过定义ID 100
启动事件过滤器哈希表开始了
# Event filter for the initial query for all "Start" events in the last 24 hours
$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
中的对象填充DataTable
,或者根据属性值生成插入查询
您是否看过使用:'SCHTASKS/Query/V' – Squashman
您有什么版本的PowerShell?你要从哪个操作系统运行?你试过了什么,因为这看起来像一个不符合SO精神的代码请求? – Matt
我可能会使用'schtasks/query/v/fo csv | ConvertFrom-Csv'。 –