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 阅读(1924) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?