查询电脑开关机时间的vbs代码

时间:2021-05-22

核心代码:

strComputer = "."Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!\\" _& strComputer & "\root\cimv2")Set colLoggedEvents = objWMIService.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 "本次开机时间: " & FormatWMIUTC(objEvent.TimeWritten) ElseIf Flag = 2 Then Wscript.Echo "上次关机时间: " & FormatWMIUTC(objEvent.TimeWritten) ElseIf Flag = 3 Then Wscript.Echo "上次开机时间: " & FormatWMIUTC(objEvent.TimeWritten) Exit For End IfNext'FormatUTCFunction FormatWMIUTC(WMIDateString) DS = " // :: " FormatWMIUTC = Left(WMIDateString,2) For i = 2 To 7 FormatWMIUTC = FormatWMIUTC & Mid(WMIDateString, i * 2 - 1, 2) & Mid(DS,i,1) Next 'FormatWMIUTC = Mid(WMIDateString, 1, 4) & "年" _ ' & Mid(WMIDateString, 5, 2) & "月" _ ' & Mid(WMIDateString, 7, 2) & "日 " _ ' & Mid (WMIDateString, 9, 2) & ":" _ ' & Mid(WMIDateString, 11, 2) & ":" _ ' & Mid(WMIDateString,13, 2)End Function

将上面的代码保存为vbs后缀的文件,双击运行即可。看到这次的开机时间,上次的开关机时间。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章