vbs查看系统开关机时间
如何获取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查看系统开关机时间的更多相关文章
- win10中查看开关机时间及查看admin的RID的方法
原文链接: https://www.toutiao.com/i6772133439593251339/ 打开系统的注册表 键盘输入win+r组合键出现运行窗口命令 输入regedit 按回车键,进入注 ...
- windows如何查看电脑开关机记录
如何查看电脑开关机记录 (一)如果你只是想查看一下,从昨天关机到今天开机之间有没有人使用我的计算机,在“开始”菜单的运行”中输入“eventvwr.msc”,或者是按下"开始菜单" ...
- windows开关机时间记录
1. 开机时间记录批处理文件,kai.bat @echo off echo %date% %time% 开机 >>D:\开关机记录\开关机时间.txt 2. 关机时间记录批处理文件,gua ...
- Linux常用命令(第二版) --系统开关机命令
系统开关机命令 说明-服务器不会经常的关机,重启,没有故障,服务器不会关机.因此这些命令就显得不是很常用. 1.shutdown /usr/sbin/shutdown e.g. shutdown -h ...
- Windows XP 如何查看计算机开关机记录
在Windows XP中,我们可以通过“事件查看器”的事件日志服务查看计算机的开.关机时间.因为事件日志服务会随计算机一起启动和关闭,并在事件日志中留下记录. 在这里有必要介绍两个ID号:6006和6 ...
- 查windows系统开关机记录
https://blog.csdn.net/dufufd/article/details/53316079 者是按下"开始菜单"-"程序"-"管理工具 ...
- Linux——系统开关机指令简单学习笔记
关机: 命令名称:shutdown 命令所在路径:/usr/sbin/shutdown 执行权限:root 语法:shutdown 功能描述:关机 范例:# shutdown -h now 重启: 命 ...
- windows系统开/关机日志位置
邮件计算机=>管理 =>系统工具=>事件查看器=>Windows日志=>系统 过滤:关机:事件ID=6006 开机:事件ID=6005
- Win 7 通过事件管理器查看计算机开机关机时间
控制面板-管理工具-事件查看器 视图中开机来源:Kernel-General 事件ID:13 关机来源:Kernel-General 事件ID:12
随机推荐
- MyBatis 插入数据库返回主键
最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...
- Js 类型方面的神坑
你有没有遇见过本来好好的一个数组结果 typeof 出来是个 object 的情况,你有没有遇到过非要写个 typeof x === "undefined" 判断未赋值的情况... ...
- PHP搜索文件夹下全部文件
搜索文件夹下全部文件 //搜索文件夹下全部文件,暂时不支持中文文件名 public function scanFile($path) { if (!is_dir($path)) return arra ...
- Django 组合索引
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...
- linux虚拟机ping通主机
右键虚拟机,选择网络适配器,设置为桥接模式.然后关闭主机防火墙,ping就行了(一直ping是没有参数的)
- 修改和重置WAMP的phpMyAdmin密码
WAMP是Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,常用来搭建动态网站或者服务器的开源软件. 1.下载页面:http://www.wampserver ...
- Servlet和Filter的url匹配以及url-pattern详解
Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单,老少皆宜.估计大多数朋友都是直接配置用,也没有关心过具体的细节,今天遇到一个问题,上网查了servlet的规范才发现,ser ...
- Luogu-3222 [HNOI2012]射箭
几何题,二次函数,化一下式子吧 设二次函数\(y=ax^2+bx\),对于一个线段\((x,y1)\),\((x,y2)\),与他相交的条件是\(y1<=ax^2+bx<=y2\) 对于\ ...
- LeetCode——palindrome-partitioning
Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...
- R语言笔记004——R批量读取txt文件
R批量读取txt文件 本文数据,代码都是参考的是大音如霜公众号,只是自己跟着做了一遍. path<-'C:\\Users\\Administrator\\Desktop\\docs' docs& ...