将小时数添加到从 Get-WinEvent 收到的 TimeCreated

Add hours to TimeCreated received from Get-WinEvent

我有以下命令:

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)

但是执行上述操作并没有给我想要的答案。它没有将时间存储为值。

需要展开事件对象的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