码迷,mamicode.com
首页 > 其他好文 > 详细

vbs查看系统开关机时间

时间:2015-06-06 17:55:18      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:

如何获取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

 

vbs查看系统开关机时间

标签:

原文地址:http://www.cnblogs.com/mq0036/p/4556817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!