时间格式转换

在WMI中使用Win32_NTLogEvent查询出来的日志生成时间timegenerated类似于“20130820162350.350000+480“,这种时间格式很特殊,经查是DMTF时间格式字符串,在PowerShell中与其它时间类型进行比较就需要对其进行转换,将DMTF字符串转DateTime类型。

转换的方案有两种:
 
1)采用.NET 2.0中的System.Management.ManagementDateTimeConverter类
该类位于system.management.dll动态链接库中,使用时需要先加载,代码如下:
[void][Reflection.Assembly]::LoadFile("c:\windows\microsoft.net\framework\v2.0.50727\system.management.dll")
gwmi win32_ntlogevent -Filter "logfile='application' and sourcename='db2' " -Property timegenerated,message -com $ip|
select Message,@{Name="LogTime";Expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.Timegenerated)}}|
sort LogTime -desc
 
2)使用事件对象的扩展方法 ConvertToDateTime
gwmi -q "SELECT timegenerated FROM win32_ntlogevent WHERE logfile='application' and sourcename='db2'"|%{$_.ConvertToDateTime($_.Timegenerated)}

 

转载于:https://www.cnblogs.com/IvanChen/p/4492358.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值