使用 Where-Object 在 Windows Server 2003 上按日期过滤 Get-WmiObject



我试图按日期过滤此查询(获取$time之后的值)。我没有在 -filter 参数执行此操作,因为我在 Windows Server 2003 SP2 上出现错误。

$colLogFiles = Get-WmiObject -Class Win32_NTLogEvent -ComputerName "localhost" | Where-Object {($_.EventType -eq "1") -or (($_.EventType -eq "2") -and ($_.TimeGenerated -gt $time))}

但是最后一个条件它没有做任何事情,我认为这是因为日期时间格式无法识别。$_ 的示例。生成时间是 20181213144843.186997-000

是否有任何方法可以执行此操作或更改该日期时间格式?

我通过使用库win32com在Python中制作它来解决它。然后我用这种日期格式添加过滤器!

time = datetime.today() + timedelta(hours=5, minutes=-10)
timeFormatted = time.strftime("%Y%m%d%H%M%S.000000-000")

系统 wmi 格式有一个 GTM 0,但我的电脑是 GTM -5,这就是我添加 5 小时的原因。

查询:

query = "Select * from Win32_NTLogEvent where (EventType = 1 or EventType = 2) and TimeGenerated >= " + """ + timeFormatted + """

2003年太有限了!

谢谢大家

最新更新