使用PS从Windows日志中提取事件时间

时间:2018-07-27 16:04:28

标签: powershell windows-scripting

我正在执行此powershell脚本,以检查Windows事件日志查看器中是否有具体的作业结果。

它正在工作,但是我只能停留在一部分上...
我想做的是拉出最近/最近的事件时间/日期,并将其添加到退出代码中。

    'strFrom = Application.CurrentProject.FullName
    'strTo = "C:\FolderName\NewFileName.accdb"

Public Sub copyFile(strFrom As String, strTo As String)
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject

    fso.copyFile strFrom, strTo

    Set fso = Nothing

End Sub

我四处张望,尝试了几种不同的方法,但都没有成功……不是PS领域的佼佼者,有人可以协助吗?

"Task Completed Successfully at **_______**"

1 个答案:

答案 0 :(得分:0)

我真的很讨厌用超长行压缩代码,直到几乎不可读为止。

由于$action(s)具有.TimeCreated属性,因此我会使用它。

这可能有效:

Param(
    [string]$Task,
    [string]$PastMinutes
)

$StartAt = (Get-Date).AddMinutes(-$PastMinutes)
$ErrorActionPreference = "SilentlyContinue"

$FilterHashTable = @{
    logname   = "Microsoft-Windows-TaskScheduler/Operational"
    id        = 102
    StartTime = $StartAt
}

$actions = (Get-WinEvent -FilterHashtable $FilterHashTable | 
    Where-Object {($_.Message -like $Task) -and 
                  ($_.Message -like "Task Scheduler Successfully Finished*")})

## set negative result hopefully overwritten by action
$Result = "CRITICAL: {0} Task Failed to Complete!" -F $Task

if ($actions){
    ForEach($action in $actions){
        $Result = "OK: {0} Task Completed Successfully at {1}" -F $Task,$action.TimeCreated
    }
}