vbs查看系统开关机时间
如何获取XP系统的开机时间?
下面给出两种代码,保存到扩展名为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) 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 'CreateObject("Wscript.Shell").Run "D:\Document\消费记录.xlsx",3,ture '此处用于测试打开文件 Function UTCtoNow(nD) If Not IsNull(nD) Then Set SWDT = CreateObject("WbemScripting.SWbemDateTime") SWDT.Value = nD UTCtoNow = SWDT.GetVarDate(True) End If End Function
二:下面在给出一个循环查找多次开机记录的方式,并且写到文件并打开 ---- 写文件形式
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'") filePath="c:\startLog.txt" set fso=createobject("scripting.filesystemobject") set file=fso.opentextfile(filePath,2,true) file.writeline "当前时间:" & Now file.writeline "最近几次的开机和关机时间:" file.close set file=fso.opentextfile("c:\startLog.txt",8,true) Flag = 0 For Each objEvent In colLoggedEvents Flag = Flag +1 msg = "" If (flag < 25) Then If (flag Mod 2) = 0 Then msg = "关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine Else msg = "开机时间:" & UTCtoNow(objEvent.TimeWritten) End If file.writeline msg End If Next file.close CreateObject("Wscript.Shell").Run filePath,3,ture Function UTCtoNow(nD) If Not IsNull(nD) Then Set SWDT = CreateObject("WbemScripting.SWbemDateTime") SWDT.Value = nD UTCtoNow = SWDT.GetVarDate(True) End If End Function
优化代码:保存的文件根据时间生成,提示保存路径,以及增加换行
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'") filePath="c:\startLog_" & FormateDateTime(Now,1) & ".txt" set fso=createobject("scripting.filesystemobject") set file=fso.opentextfile(filePath,2,true) file.writeline "当前系统时间:" & Now file.writeline "最近几次的开机和关机时间:" & vbNewLine file.close set file=fso.opentextfile(filePath,8,true) Wscript.Echo "开机日志已保存到:" & vbNewLine & filePath Flag = 0 For Each objEvent In colLoggedEvents Flag = Flag +1 msg = "" If (flag < 25) Then If (flag Mod 2) = 0 Then msg = "关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine Else msg = "开机时间:" & UTCtoNow(objEvent.TimeWritten) End If file.writeline msg End If Next file.close CreateObject("Wscript.Shell").Run filePath,3,ture 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 FormateDateTime(strTime,ParaType) select case ParaType Rem Type 1 is YYYYMMDDHHmmss case "1" strTime = year(strTime) & right( "00" & month(strTime),2) & right( "00" & day(strTime),2) & right( "00 " & hour(strTime),2) & right( "00 " & minute(strTime),2) & right( "00 " & second(strTime),2) Rem Type 2 is YYYYMMDD case "2" strTime = year(strTime) & right( "00" & month(strTime),2) & right( "00" & day(strTime),2) Rem Type 3 is YYYY-MM-DD case "3" strTime = year(strTime) & "-"& right( "00" & month(strTime),2) & "-"& right( "00" & day(strTime),2) Rem Type 4 is YYYY年MM月DD日 case "4" strTime = year(strTime) & "年"& right( "00 " & month(strTime),2) & "月"& right( "00" & day(strTime),2)& "日 " Rem Type 5 is YYYY-MM-DD HH:mm:ss case "5" strTime = year(strTime) & "-"& right( "00 " & month(strTime),2) & "-"& right( "00" & day(strTime),2) & " "& right( "00 " & hour(strTime),2) & ": "& right( "00 " & minute(strTime),2) & ": "& right( "00 " & second(strTime),2) end select FormateDateTime = strTime end Function
优化代码:去除FormateDateTime函数中的部分空格
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'") filePath="c:\startLog_" & FormateDateTime(Now,1) & ".txt" set fso=createobject("scripting.filesystemobject") set file=fso.opentextfile(filePath,2,true) file.writeline "当前系统时间:" & Now file.writeline "最近几次的开机和关机时间:" & vbNewLine file.close set file=fso.opentextfile(filePath,8,true) Wscript.Echo "开机日志已保存到:" & vbNewLine & filePath Flag = 0 For Each objEvent In colLoggedEvents Flag = Flag +1 msg = "" If (flag < 25) Then If (flag Mod 2) = 0 Then msg = "关机时间:" & UTCtoNow(objEvent.TimeWritten) & vbNewLine Else msg = "开机时间:" & UTCtoNow(objEvent.TimeWritten) End If file.writeline msg End If Next file.close CreateObject("Wscript.Shell").Run filePath,3,ture 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 FormateDateTime(strTime,ParaType) select case ParaType Rem Type 1 is YYYYMMDDHHmmss case "1" strTime = year(strTime) & right( "00" & month(strTime),2) & right( "00" & day(strTime),2) & right( "00" & hour(strTime),2) & right( "00" & minute(strTime),2) & right( "00" & second(strTime),2) Rem Type 2 is YYYYMMDD case "2" strTime = year(strTime) & right( "00" & month(strTime),2) & right( "00" & day(strTime),2) Rem Type 3 is YYYY-MM-DD case "3" strTime = year(strTime) & "-"& right( "00" & month(strTime),2) & "-"& right( "00" & day(strTime),2) Rem Type 4 is YYYY年MM月DD日 case "4" strTime = year(strTime) & "年"& right( "00" & month(strTime),2) & "月"& right( "00" & day(strTime),2)& "日" Rem Type 5 is YYYY-MM-DD HH:mm:ss case "5" strTime = year(strTime) & "-"& right( "00" & month(strTime),2) & "-"& right( "00" & day(strTime),2) & " "& right( "00" & hour(strTime),2) & ":"& right( "00" & minute(strTime),2) & ":"& right( "00" & second(strTime),2) end select FormateDateTime = strTime end Function
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/4556817.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2015-06-06 16:14 jack_Meng 阅读(1918) 评论(0) 编辑 收藏 举报