如何获取 Windows 上次重启原因
我想知道提供有关上次 Windows 重新启动源信息的 Windows API 函数(如果存在)是什么。可能的原因主要有以下三个:
- 计算机因蓝屏崩溃
- 用户或程序关闭/重新启动计算机
- 断电
我能得到的详细信息越多越好。不过,我至少需要知道主要是哪个原因。
我需要支持Windows Vista和Windows 7。
答:
似乎没有直接的API来获取该信息。相反,我们必须收集 Windows 事件日志。系统重新启动信息位于事件查看器/Windows 日志/系统中。以下是事件 ID 提供的各种信息:
- 6005:Windows 启动
- 6006:Windows 关闭(正常)
- 6008:Windows 关闭(意外)
我还没有弄清楚断电和系统崩溃之间的区别,但这是一个好的开始。
I'd like to know what is the Windows API function (if any exists) that provides information about the last Windows reboot source. There are three main possible causes:
- The computer crashed on a blue screen
- A user or a program shutdown/restarted the computer
- A power lost
The more details I can get the better. However, I need to know at least which reason it is from the main ones.
I need to support Windows Vista and Windows 7.
Answer:
It seems that there is no direct API to get that information. Instead, we have to harvest the Windows Event Log. System reboot information is located in Event Viewer/Windows Logs/System. Here is the various information provided by the event ids:
- 6005: Windows start-up
- 6006: Windows shutdown (properly)
- 6008: Windows shutdown (unexpectedly)
I do not yet get the difference between power lost and system crash, but it's a good start.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这篇文章详细解释了如何查找上次启动/关闭的原因。就我而言,这是由于 Windows SCCM 推送更新,即使我在本地禁用了它。请访问该文章以获取带有图片的完整详细信息。作为参考,以下是从网站复制/粘贴的步骤:
其他有用的事件 ID(来源)
This article explains in detail how to find the reason for last startup/shutdown. In my case, this was due to windows SCCM pushing updates even though I had it disabled locally. Visit the article for full details with pictures. For reference, here are the steps copy/pasted from the website:
Other useful event IDs (source)
查看事件日志 API。情况 a)(蓝屏、用户切断电源线或系统挂起)会导致下次系统正确重新启动时,在“系统”事件日志中留下一条注释(“系统未正确关闭”或类似内容)。您应该能够使用上述 API 以编程方式访问它(老实说,我从未使用过它,但它应该可以工作)。
Take a look at the Event Log API. Case a) (bluescreen, user cut the power cord or system hang) causes a note ('system did not shutdown correctly' or something like that) to be left in the 'System' event log the next time the system is rebooted properly. You should be able to access it programmatically using the above API (honestly, I've never used it but it should work).
您可以使用以下 powershell 脚本自动执行过去 5 天的调查:
You may automate your investigation for the last 5 days with this powershell script:
有一个简单的方法使用 powershell。
powershell "Get-WinEvent -FilterHashtable @{logname = 'System'; id = 1074, 6005, 6006, 6008} -MaxEvents 6 | Format-Table -wrap"
您也可以设置要显示的最大事件数。
There is a simple way using powershell.
powershell "Get-WinEvent -FilterHashtable @{logname = 'System'; id = 1074, 6005, 6006, 6008} -MaxEvents 6 | Format-Table -wrap"
you can set the max events to display too.