Copy codeThe code is as follows:
If (Lcase(Right(Wscript.FullName,11)) = “wscript.exe”) Then
CreateObject(“WScript.Shell”). Run(“%Comspec% /C ” &Chr(34)&”mode con cols=100&Cscript.exe //NoLogo “&Chr(34)& Wscript. Scriptfullname & Chr (34) & “& & (echo this window closes automatically after 40 seconds… & Ping – N 40 127.0.1 > nul & exit)” & Chr (34)), 3: Wscript Quit
Wscript.Quit
End If
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’ Or EventCode = ‘6008’”)
For Each objEvent In colLoggedEvents
Flag = Flag + 1
If Flag = 1 Then
Wscript. Echo “this boot time:” & utctonow (objeevent. Timewritten)
ElseIf (Flag Mod 2) = 0 Then
G = UTCtoNow(objEvent.TimeWritten)
Else
K = UTCtoNow(objEvent.TimeWritten)
Wscript. Echo “previous power on:” & K & “” & vbtab & “corresponding power off:” & G & “” & vbtab & “run time:” & stohms (DateDiff (“s, K, g))
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
Function StoHMS(Sec)
H = Int(Sec/3600) :H1 = Sec Mod 3600:M = Int(H1/60) :S = H1 Mod 60
Stohms = H & “hours” & M & “minutes” & S & “seconds”
End Function