VBS _获取系统本次及上次开关机时间WinXP Win7兼容版
VBS _获取系统本次及上次开关机时间WinXP Win7兼容版
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colLoggedEvents = WMI.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' And EventCode = '6005' Or EventCode = '6006'")
For Each objEvent In colLoggedEvents
Flag = Flag +1
If Flag = 1 Then
Wscript.Echo "本次开机时间: " & UTCtoNow(objEvent.TimeWritten)
Else
If (flag < 4) Then
If (flag Mod 2) = 0 Then
G = "上次关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine
Else
K = "上次开机时间:" & UTCtoNow(objEvent.TimeWritten)
Wscript.Echo K & vbNewLine & G
End If
End If
End If
Next
Function UTCtoNow(nD)
If Not IsNull(nD) Then
Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
SWDT.Value = nD
UTCtoNow = SWDT.GetVarDate(True)
End If
End Function
把以上代码保存扩展名是vbs的文件就可以了。
参考:http://hi.baidu.com/haikqq/blog/item/fed2f7d745499dc6a044df15.html
根据以上代码和构思,我经过自己的修改和优化,比较符合自己的使用习惯,你可以把下面的代码保存到扩展名为vbs的文件中,如下代码:
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set colLoggedEvents = WMI.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' And EventCode = '6005' Or EventCode = '6006'") For Each objEvent In colLoggedEvents Flag = Flag +1 If Flag = 1 Then Wscript.Echo "本次开机时间: " & UTCtoNow(objEvent.TimeWritten) end if If (flag>=2 and flag < 4) Then If (flag Mod 2) = 0 Then G = "上次关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine Else K = "上次开机时间:" & UTCtoNow(objEvent.TimeWritten) Wscript.Echo K & vbNewLine & G End If End If If (flag>=4 and flag <6) Then If (flag Mod 2) = 0 Then G = "再上次关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine Else K = "再上次开机时间:" & UTCtoNow(objEvent.TimeWritten) Wscript.Echo K & vbNewLine & G End If End If If (flag Mod 2) = 1 Then G=K="" END IF Next Function UTCtoNow(nD) If Not IsNull(nD) Then Set SWDT = CreateObject("WbemScripting.SWbemDateTime") SWDT.Value = nD UTCtoNow = SWDT.GetVarDate(True) End If End Function
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/archive/2012/12/03/2799150.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!