• vbs查看系统开关机时间


    如何获取XP系统的开机时间?

    下面给出两种代码,保存到扩展名为vbs的文件中。具体哪个请根据自己需求决定。

    一:根据系统日志,查看开机时间和关机时间,---- 使用弹出对话框的形式

    Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\.
    ootcimv2")
    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}!\.
    ootcimv2")
    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
    View Code

    以下是经过优化代码:保存的文件根据时间生成,提示保存路径,以及增加换行

    Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\.
    ootcimv2")
    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 
    View Code
  • 相关阅读:
    11.8-ros-navigation解析
    8.14-rqt_common_pluggins 详解
    8.1-roscomm详解
    8.15-ros-bag使用
    7.26-rosbridge-suit 解读
    7.26-roscpp_overview详解
    7.26-ROS其他有价值模块
    java dbutils查询数据库时无法给部分字段赋值原因
    java_获取多个文件夹下所有.java源码的总行数
    正则表达式-1-初识正则表达式
  • 原文地址:https://www.cnblogs.com/mq0036/p/4556817.html
Copyright © 2020-2023  润新知