vbs查看系统开关机时间
2015-06-06 16:14
281 查看
如何获取XP系统的开机时间?
下面给出两种代码,保存到扩展名为vbs的文件中。具体哪个请根据自己需求决定。
一:根据系统日志,查看开机时间和关机时间,---- 使用弹出对话框的形式
二:下面在给出一个循环查找多次开机记录的方式,并且写到文件并打开 ---- 写文件形式
下面给出两种代码,保存到扩展名为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
相关文章推荐
- 【VB.NET机房重构】编程注释
- vb timer控件
- VBA设置单元格格式
- 【VB.NET机房重构】从字符串到类型double的转换无效?
- 关于 Excel 2003 vba usedrange属性
- opengl顶点数据传送和着色器处理(vao,vbo)
- 如何在Excel中使用VB宏连接SAP系统
- 【VB.NET机房重构】ADO.NET
- vb里面图片怎么使按钮上面图片交替显示的代码
- 下了个蓝屏代码查看工具,就中病毒了。。。什么鬼病毒,竟然还是用的VBS
- ubuntu rmvb
- 安装VirtualBox出错提示Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790
- CvHaarTrainingData和CvBackgroundData的区别
- win7下面安装One lingo.msi出现Error:2738 Could not access VBScript run time for custom action
- 利用vbs 运行程序 并使窗口隐藏
- 在VB.net 2010 中怎样使用Halcon
- VB.NET vs. C#, round 2: Pounding on performance--VB.NET vs C#, 第二波:效率碰撞
- 如何在Vblock里配置Boot from SAN
- 演练:Office 编程(C# 和 Visual Basic)
- VB.net进阶培训及阶段总结