从Get-WinEvent

时间:2016-01-09 11:48:28

标签: powershell datetime logging event-log

我有以下命令:

Get-WinEvent -FilterHashtable @{
  Logname      = 'Application'
  ID           = 1000
  ProviderName = 'Application Error'
} -MaxEvents 1 | select TimeCreated

我得到以下输出:

TimeCreated
-----------
04-Jan-16 11:29:11 PM

我希望将此值保存到变量中并添加两个小时,如下所示:

$ErrorTime = Get-WinEvent -FilterHashtable @{
               Logname      = 'Application'
               ID           = 1000
               ProviderName = 'Application Error'
             } -MaxEvents 1 | select TimeCreated

$Time1 = ($ErrorTime).AddHours(2)

但是做到这一点并没有给我想要的答案。它不会将时间存储为值。

1 个答案:

答案 0 :(得分:1)

您需要展开事件对象的TimeCreated属性才能获得DateTime值:

$ErrorTime = Get-WinEvent -FilterHashtable @{
               Logname      = 'Application'
               ID           = 1000
               ProviderName = 'Application Error'
             } -MaxEvents 1 | Select-Object -Expand TimeCreated

$Time1 = $ErrorTime.AddHours(2)

您可以通过对象的DateTime属性获取时间戳的日期和时间部分:

$date = $Time1.Date
$time = $Time1.Time