• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    查询电脑开关机时间的vbs代码

    发布者: 怀卉1097 | 发布时间: 2025-8-14 00:25| 查看数: 77| 评论数: 0|帖子模式

    核心代码:
    1. strComputer = "."
    2. Set objWMIService = GetObject("winmgmts:" _
    3. & "{impersonationLevel=impersonate}!\" _
    4. & strComputer & "\root\cimv2")
    5. Set colLoggedEvents = objWMIService.ExecQuery _
    6. ("Select * from Win32_NTLogEvent " _
    7. & "Where Logfile = 'System' And EventCode = '6005' Or EventCode = '6006'")
    8. For Each objEvent In colLoggedEvents
    9.   Flag = Flag + 1
    10.   If Flag = 1 Then
    11.     Wscript.Echo "本次开机时间: " & FormatWMIUTC(objEvent.TimeWritten)
    12.   ElseIf Flag = 2 Then
    13.     Wscript.Echo "上次关机时间: " & FormatWMIUTC(objEvent.TimeWritten)
    14.   ElseIf Flag = 3 Then
    15.     Wscript.Echo "上次开机时间: " & FormatWMIUTC(objEvent.TimeWritten)
    16.     Exit For
    17.   End If
    18. Next

    19. 'FormatUTC
    20. Function FormatWMIUTC(WMIDateString)
    21.   DS = " // :: "
    22.   FormatWMIUTC = Left(WMIDateString,2)
    23.   For i = 2 To 7
    24.     FormatWMIUTC = FormatWMIUTC & Mid(WMIDateString, i * 2 - 1, 2) & Mid(DS,i,1)
    25.   Next
    26.   'FormatWMIUTC = Mid(WMIDateString, 1, 4) & "年" _
    27.   '      & Mid(WMIDateString, 5, 2) & "月" _
    28.   '      & Mid(WMIDateString, 7, 2) & "日 " _
    29.   '      & Mid (WMIDateString, 9, 2) & ":" _
    30.   '      & Mid(WMIDateString, 11, 2) & ":" _
    31.   '      & Mid(WMIDateString,13, 2)
    32. End Function
    复制代码
    将上面的代码保存为vbs后缀的文件,双击运行即可。看到这次的开机时间,上次的开关机时间。


    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表