【发布时间】:2021-02-27 18:27:13
【问题描述】:
我有一个 Powershell 脚本如下:
if (something)
{
# do something
# will return 0 on success
}
else
{
exit 12345
}
我希望能够检查在过去 24 小时内至少有一次成功(返回代码 0)(脚本将返回 12345 多于 0)
在另一个脚本中我有这个代码:
$events = @(
Get-WinEvent -FilterXml @'
<QueryList>
<Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
<Select Path="Microsoft-Windows-TaskScheduler/Operational">
*[EventData/Data[@Name='taskname']='\My Test']
</Select>
</Query>
</QueryList>
'@ -ErrorAction Stop
$events | Where-Object {$_.ID -eq 102} | Select-Object *
它向我显示了任务运行的历史记录,但我找不到如何从这里获得运行结果。
我可以如下查询单个历史项目:
$a = $events | Where-Object {$_.ID -eq 102} | Select-Object *
$a[0] | Get-Member
返回
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
ActivityId NoteProperty guid ActivityId=d6ac8489-c0e1-4dbd-b06e-7ecefaf1c20c
Bookmark NoteProperty EventBookmark Bookmark=System.Diagnostics.Eventing.Reader.EventBookmark
ContainerLog NoteProperty string ContainerLog=Microsoft-Windows-TaskScheduler/Operational
Id NoteProperty int Id=102
Keywords NoteProperty long Keywords=-9223372036854775807
KeywordsDisplayNames NoteProperty ReadOnlyCollection[string] KeywordsDisplayNames=System.Collections.ObjectModel.ReadOnlyCollection`1[System.String]
Level NoteProperty byte Level=4
LevelDisplayName NoteProperty string LevelDisplayName=Information
LogName NoteProperty string LogName=Microsoft-Windows-TaskScheduler/Operational
MachineName NoteProperty string MachineName=MyPC.mydomain
MatchedQueryIds NoteProperty uint32[] MatchedQueryIds=System.UInt32[]
Message NoteProperty string Message=Task Scheduler successfully finished "{d6ac8489-c0e1-4dbd-b06e-7ecefaf1c20c}" instance of the "\My Test" task for user "MyD...
Opcode NoteProperty int16 Opcode=2
OpcodeDisplayName NoteProperty string OpcodeDisplayName=Stop
ProcessId NoteProperty int ProcessId=2544
Properties NoteProperty List[EventProperty] Properties=System.Collections.Generic.List`1[System.Diagnostics.Eventing.Reader.EventProperty]
ProviderId NoteProperty guid ProviderId=de7b24ea-73c8-4a09-985d-5bdadcfa9017
ProviderName NoteProperty string ProviderName=Microsoft-Windows-TaskScheduler
Qualifiers NoteProperty object Qualifiers=null
RecordId NoteProperty long RecordId=21093
RelatedActivityId NoteProperty object RelatedActivityId=null
Task NoteProperty int Task=102
TaskDisplayName NoteProperty string TaskDisplayName=Task completed
ThreadId NoteProperty int ThreadId=14152
TimeCreated NoteProperty datetime TimeCreated=16/11/2020 13:26:20
UserId NoteProperty SecurityIdentifier UserId=S-1-5-18
Version NoteProperty byte Version=0
但是我在任何属性中都找不到我需要的信息。我希望它以十六进制格式存储在某处(0x3039)
【问题讨论】:
标签: powershell windows-10 scheduled-tasks