欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-07 15:26:36
核心代码: strcomputer = "." set objwmiservice = getobject("winmgmts:" _ & "{imperso...

核心代码:

strcomputer = "."
set objwmiservice = getobject("winmgmts:" _
& "{impersonationlevel=impersonate}!\\" _
& strcomputer & "\root\cimv2")
set colloggedevents = objwmiservice.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 "本次开机时间: " & formatwmiutc(objevent.timewritten)
  elseif flag = 2 then
    wscript.echo "上次关机时间: " & formatwmiutc(objevent.timewritten)
  elseif flag = 3 then
    wscript.echo "上次开机时间: " & formatwmiutc(objevent.timewritten)
    exit for
  end if
next

'formatutc
function formatwmiutc(wmidatestring)
  ds = " // :: "
  formatwmiutc = left(wmidatestring,2)
  for i = 2 to 7
    formatwmiutc = formatwmiutc & mid(wmidatestring, i * 2 - 1, 2) & mid(ds,i,1)
  next
  'formatwmiutc = mid(wmidatestring, 1, 4) & "年" _
  '      & mid(wmidatestring, 5, 2) & "月" _
  '      & mid(wmidatestring, 7, 2) & "日 " _
  '      & mid (wmidatestring, 9, 2) & ":" _
  '      & mid(wmidatestring, 11, 2) & ":" _
  '      & mid(wmidatestring,13, 2)
end function

将上面的代码保存为vbs后缀的文件,双击运行即可。看到这次的开机时间,上次的开关机时间。

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

相关标签: vbs 开关机时间