如何获取 Windows 上次重启原因

发布于 2024-08-19 01:56:31 字数 462 浏览 3 评论 0原文

我想知道提供有关上次 Windows 重新启动源信息的 Windows API 函数(如果存在)是什么。可能的原因主要有以下三个:

  1. 计算机因蓝屏崩溃
  2. 用户或程序关闭/重新启动计算机
  3. 断电

我能得到的详细信息越多越好。不过,我至少需要知道主要是哪个原因。

我需要支持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:

  1. The computer crashed on a blue screen
  2. A user or a program shutdown/restarted the computer
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸里的快感 2024-08-26 01:56:31

这篇文章详细解释了如何查找上次启动/关闭的原因。就我而言,这是由于 Windows SCCM 推送更新,即使我在本地禁用了它。请访问该文章以获取带有图片的完整详细信息。作为参考,以下是从网站复制/粘贴的步骤:

  1. Windows + R 键打开运行对话框,输入 eventvwr.msc ,然后按Enter

  2. 如果出现 UAC 提示,请单击/点按“是”(Windows 7/8) 或“继续”(Vista)。

  3. 事件查看器的左窗格中,双击/点击Windows日志将其展开,单击系统以展开选择它,然后右键单击
    系统,然后单击/点击过滤当前日志

  4. 执行下面的第 5 步或第 6 步,了解您希望看到的关闭事件。

  5. 查看所有用户关闭计算机的日期和时间

    A) 在事件源中,单击/点击下拉箭头并选中USER32框。

    B) 在所有事件 ID 字段中,输入 1074,然后单击/点击确定

    C) 这将为您提供关闭电源(关机)和重新启动的列表
    事件查看器中间窗格顶部的关闭类型事件。

    D) 您可以滚动浏览这些列出的事件以查找事件
    以 power off 作为关机类型。您会注意到日期和
    时间,以及负责关闭计算机的用户
    列出断电事件。

    E) 转到步骤 7。

  6. 查看计算机所有意外关闭的日期和时间

    A.在所有事件 ID 字段中输入 6008,然后单击/点击确定

    B.这将为您提供意外关闭事件的列表
    事件查看器中的中间窗格顶部。您可以滚动浏览这些
    列出事件以查看每个事件的日期和时间。

  7. 完成后,您可以关闭事件查看器。


其他有用的事件 ID(来源

ID描述
41系统在未先完全关闭的情况下重新启动。
1074系统已被用户或进程正常关闭。
1076跟在事件 ID 6008 之后,表示在意外重启或关闭后第一个具有关闭权限的用户登录到服务器并指定原因。
6005事件日志服务已启动。表示系统启动。
6006事件日志服务已停止。指示系统正确关闭。
6008上次系统意外关闭。
6009系统启动时检测到的操作系统版本。
6013系统正常运行时间(以秒为单位)。

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:

  1. Press the Windows + R keys to open the Run dialog, type eventvwr.msc, and press Enter.

  2. If prompted by UAC, then click/tap on Yes (Windows 7/8) or Continue (Vista).

  3. In the left pane of Event Viewer, double click/tap on Windows Logs to expand it, click on System to select it, then right click on
    System, and click/tap on Filter Current Log.

  4. Do either step 5 or 6 below for what shutdown events you would like to see.

  5. To see the dates and times of all user shut downs of the computer

    A) In Event sources, click/tap on the drop down arrow and check the USER32 box.

    B) In the All Event IDs field, type 1074, then click/tap on OK.

    C) This will give you a list of power off (shutdown) and restart
    shutdown type of events at the top of the middle pane in Event Viewer.

    D) You can scroll through these listed events to find the events
    with power off as the shutdown type. You will notice the date and
    time, and what user was responsible for shutting down the computer per
    power off event listed.

    E) Go to step 7.

  6. To see the dates and times of all unexpected shut downs of the computer

    A. In the All Event IDs field type 6008, then click/tap on OK.

    B. This will give you a list of unexpected shutdown events at the
    top of the middle pane in Event Viewer. You can scroll through these
    listed events to see the date and time of each one.

  7. When finished, you can close Event Viewer.


Other useful event IDs (source)

IDDescription
41The system has rebooted without cleanly shutting down first.
1074The system has been shutdown properly by a user or process.
1076Follows after Event ID 6008 and means that the first user with shutdown privileges logged on to the server after an unexpected restart or shutdown and specified the cause.
6005The Event Log service was started. Indicates the system startup.
6006The Event Log service was stopped. Indicates the proper system shutdown.
6008The previous system shutdown was unexpected.
6009The operating system version detected at the system startup.
6013The system uptime in seconds.
何必那么矫情 2024-08-26 01:56:31

查看事件日志 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).

樱娆 2024-08-26 01:56:31

您可以使用以下 powershell 脚本自动执行过去 5 天的调查:

$today = Get-Date
$startDay = $today.AddDays(-5)
$eventIds=(6005,6006,6008,6009,1074,1076,12,13,43,109)
$systEvents=Get-WinEvent -LogName System 
$rebootEvents=$systEvents| Where-Object {$_.TimeCreated -gt $startDay} | Where-Object {$_.Id -in $eventIds}  
format-table TimeCreated,Id,Message -AutoSize -wrap -InputObject $rebootEvents

在此处输入图像描述

You may automate your investigation for the last 5 days with this powershell script:

$today = Get-Date
$startDay = $today.AddDays(-5)
$eventIds=(6005,6006,6008,6009,1074,1076,12,13,43,109)
$systEvents=Get-WinEvent -LogName System 
$rebootEvents=$systEvents| Where-Object {$_.TimeCreated -gt $startDay} | Where-Object {$_.Id -in $eventIds}  
format-table TimeCreated,Id,Message -AutoSize -wrap -InputObject $rebootEvents

enter image description here

ゞ花落谁相伴 2024-08-26 01:56:31

有一个简单的方法使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文