VBS _获取系统本次及上次开关机时间WinXP Win7兼容版

VBS _获取系统本次及上次开关机时间WinXP Win7兼容版

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

 

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://hi.baidu.com/haikqq/blog/item/fed2f7d745499dc6a044df15.html

根据以上代码和构思,我经过自己的修改和优化,比较符合自己的使用习惯,你可以把下面的代码保存到扩展名为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)
    end if
    If (flag>=2 and 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
    If (flag>=4 and flag <6) 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

    If (flag Mod 2) = 1 Then
    G=K=""
    END IF
Next


Function UTCtoNow(nD)
    If Not IsNull(nD) Then
    Set SWDT = CreateObject("WbemScripting.SWbemDateTime")
    SWDT.Value = nD
    UTCtoNow = SWDT.GetVarDate(True)
    End If
End Function

 

你可能感兴趣的:(win7)