【问题标题】:Filtering Get-WmiObject by date on Windows Server 2003 with Where-Object在 Windows Server 2003 上使用 Where-Object 按日期过滤 Get-WmiObject
【发布时间】:2019-06-08 11:44:10
【问题描述】:

我正在尝试按日期过滤此查询(在 $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))}

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

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

【问题讨论】:

  • 尝试用$_.ConvertToDateTime($_.TimeGenerated)替换$_.TimeGenerated
  • 使用Get-WmiObject -Class Win32_NTLogEvent -ComputerName "localhost" | Select-Object -Property timegenerated | gm 发现TimeGenerated 属性是String 类型。
  • @boxdog 调用“ParseExact”时出现异常:“无法将字符串识别为有效的 DateTime 值。”
  • @lit 它是:timegenerated NoteProperty string timegenerated=20190114135011.640597-000
  • 建议不要打电话给ParseExact。使用对象本身的特殊ConvertToDateTime 方法。 (并确保$time 是有效的DateTime,或可转换为它。)请注意,由于字符串的形式为YYYYMMDD,它们确实按字典顺序排列:-gt '20190114',用于例如,即使比较不是作为日期/时间值进行,也会起作用。

标签: powershell datetime powershell-2.0 windows-server-2003


【解决方案1】:

我通过在 Python 中使用库 win32com 解决了这个问题。然后我添加一个具有这种日期格式的过滤器!

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 年太有限了!

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多