Board logo

标题: [原创] VBS 获取系统本次及上次开关机时间WinXP Win7兼容版 [打印本页]

作者: fastslz    时间: 2011-8-20 14:40     标题: VBS 获取系统本次及上次开关机时间WinXP Win7兼容版

本帖最后由 fastslz 于 2011-8-26 19:23 编辑
  1. If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
  2.    CreateObject("WScript.Shell").Run("%Comspec% /C " &Chr(34)&"mode con cols=100&Cscript.exe //NoLogo "&Chr(34)& Wscript.ScriptFullName &Chr(34)&"&&(Echo 此窗口40秒后自动关闭...&Ping -n 40 127.0.1>nul&Exit)"&Chr(34)),3:Wscript.Quit
  3.    Wscript.Quit
  4. End If
  5. Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  6. Set colLoggedEvents = WMI.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' And EventCode = '6005' Or EventCode = '6006' Or EventCode = '6008'")
  7. For Each objEvent In colLoggedEvents
  8.     Flag = Flag + 1
  9.     If Flag = 1 Then
  10.         Wscript.Echo "本次开机时间: " & UTCtoNow(objEvent.TimeWritten)
  11.         ElseIf (Flag Mod 2) = 0  Then
  12.             G = UTCtoNow(objEvent.TimeWritten)
  13.             Else
  14.             K = UTCtoNow(objEvent.TimeWritten)
  15.             Wscript.Echo "前次开机:"&K&"  "&vbTab&"对应关机:"&G&"  "&vbTab& "运行时长:" &StoHMS(DateDiff("s", K, G))
  16.     End If
  17. Next
  18. Function UTCtoNow(nD)
  19.     If Not IsNull(nD) Then
  20.     Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
  21.     SWDT.Value = nD
  22.     UTCtoNow = SWDT.GetVarDate(True)
  23.     End If
  24. End Function
  25. Function StoHMS(Sec)
  26. H = Int(Sec/3600) :H1 = Sec Mod 3600:M = Int(H1/60) :S = H1 Mod 60
  27. StoHMS = H & "小时" & M & "分钟" & S &"秒"
  28. End Function
复制代码

作者: 极品小猫    时间: 2013-1-10 18:12

收藏一下,这个不错,挖到好东西。
只可惜获取数据的耗时是个大问题




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2