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

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

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_" & FormateDateTime(Now,) & ".txt"
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile(filePath,,true)
file.writeline "当前系统时间:" & Now
file.writeline "最近几次的开机和关机时间:" & vbNewLine
file.close
set file=fso.opentextfile(filePath,,true)
Wscript.Echo "开机日志已保存到:" & vbNewLine & filePath Flag =
For Each objEvent In colLoggedEvents
Flag = Flag +
msg = ""
If (flag < ) Then
If (flag Mod ) = 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,,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 ""
strTime = year(strTime) & right( "" & month(strTime),) & right( "" & day(strTime),) & right( "00 " & hour(strTime),) & right( "00 " & minute(strTime),) & right( "00 " & second(strTime),)
Rem Type 2 is YYYYMMDD
case ""
strTime = year(strTime) & right( "" & month(strTime),) & right( "" & day(strTime),)
Rem Type 3 is YYYY-MM-DD
case ""
strTime = year(strTime) & "-"& right( "" & month(strTime),) & "-"& right( "" & day(strTime),)
Rem Type 4 is YYYY年MM月DD日
case ""
strTime = year(strTime) & "年"& right( "00 " & month(strTime),) & "月"& right( "" & day(strTime),)& "日 "
Rem Type 5 is YYYY-MM-DD HH:mm:ss
case ""
strTime = year(strTime) & "-"& right( "00 " & month(strTime),) & "-"& right( "" & day(strTime),) & " "& right( "00 " & hour(strTime),) & ": "& right( "00 " & minute(strTime),) & ": "& right( "00 " & second(strTime),)
end select
FormateDateTime = strTime
end Function

vbs查看系统开关机时间的更多相关文章

  1. win10中查看开关机时间及查看admin的RID的方法

    原文链接: https://www.toutiao.com/i6772133439593251339/ 打开系统的注册表 键盘输入win+r组合键出现运行窗口命令 输入regedit 按回车键,进入注 ...

  2. windows如何查看电脑开关机记录

    如何查看电脑开关机记录 (一)如果你只是想查看一下,从昨天关机到今天开机之间有没有人使用我的计算机,在“开始”菜单的运行”中输入“eventvwr.msc”,或者是按下"开始菜单" ...

  3. windows开关机时间记录

    1. 开机时间记录批处理文件,kai.bat @echo off echo %date% %time% 开机 >>D:\开关机记录\开关机时间.txt 2. 关机时间记录批处理文件,gua ...

  4. Linux常用命令(第二版) --系统开关机命令

    系统开关机命令 说明-服务器不会经常的关机,重启,没有故障,服务器不会关机.因此这些命令就显得不是很常用. 1.shutdown /usr/sbin/shutdown e.g. shutdown -h ...

  5. Windows XP 如何查看计算机开关机记录

    在Windows XP中,我们可以通过“事件查看器”的事件日志服务查看计算机的开.关机时间.因为事件日志服务会随计算机一起启动和关闭,并在事件日志中留下记录. 在这里有必要介绍两个ID号:6006和6 ...

  6. 查windows系统开关机记录

    https://blog.csdn.net/dufufd/article/details/53316079 者是按下"开始菜单"-"程序"-"管理工具 ...

  7. Linux——系统开关机指令简单学习笔记

    关机: 命令名称:shutdown 命令所在路径:/usr/sbin/shutdown 执行权限:root 语法:shutdown 功能描述:关机 范例:# shutdown -h now 重启: 命 ...

  8. windows系统开/关机日志位置

    邮件计算机=>管理 =>系统工具=>事件查看器=>Windows日志=>系统 过滤:关机:事件ID=6006 开机:事件ID=6005

  9. Win 7 通过事件管理器查看计算机开机关机时间

    控制面板-管理工具-事件查看器 视图中开机来源:Kernel-General 事件ID:13 关机来源:Kernel-General 事件ID:12

随机推荐

  1. 每天一个Linux命令(62)rcp命令

        rcp代表"remote file copy"(远程文件拷贝).     (1)用法:     用法:  rcp [参数] [源文件] [目标文件]     (2)功能: ...

  2. 【HackerRank】 Find Digits

    Find Digits Problem Statement Given a number you have to print how many digits in that number exactl ...

  3. Kubernetes 部署Mysql 主从复制集群

    Mysql主从参考文章: https://www.jianshu.com/p/509b65e9a4f5 http://blog.51cto.com/ylw6006/2071864 Statefulse ...

  4. processing学习整理---Structure

    1.语法介绍:与java很相近,可以认为就是java. 2.运行命令(linux): processing-java --output=/tmp/processing-xx --run --force ...

  5. Linux嵌入式 -- 内核 - 系统调用

    1. 系统调用 定义 Linux内核中设置了一组用于实现各种系统功能的子程序,称为系统调用.用户可以通过系统调用命令在自己的应用程序中调用它们. 系统调用和普通的函数调用非常相似,区别仅仅在于,系统调 ...

  6. sqlserver设置具体的访问权限

    为具体的用户设置具体的访问权限 收藏 use 你的库名 go --新增用户 exec sp_addlogin 'test' --添加登录 exec sp_grantdbaccess N'test' - ...

  7. rsync工具

    rsync工具 一.介绍 1.可以实现 本地数据 <----------> 远程数据/本地数据  的传输 2.两种通信方式(man rsync)  (1)remote shell(一个冒号 ...

  8. Struts2学习(1)

    struts2概述 1.struts2框架应用javaee三层结构中web层框架. 2.strut2框架在struts1和webwork基础之上发展全新的框架. 3.struts2解决的问题: 4.版 ...

  9. review28

    前面介绍了指向文件的输入流和输出流.随机流是既能读文件也能写文件. RandomAccessFile类创建的流称做随机流,与前面的输入.输出流不同的是,RandomAccessFile类既不是Inpu ...

  10. 本地的html服务

    本地的调试的时候, 我们服务器返回的cookie就会变的失效,因为你的本地服务器的域名不太对.