MSSQL Downtime
Uptime/Downtime
To find out if SQL Server is down...
services.msc
or (via Powershell)...
Get-Service | Out-String -Stream | Select-String -pattern "SQL"
To find out how long SQL Server has been up...
SELECT sqlserver_start_time
FROM sys.dm_os_sys_info;
or
SELECT create_date AS SQLServerStartTime
FROM sys.databases
WHERE name = 'tempdb';
To find out who restarted Windows...
eventvwr
Windows Logs - System - Filter Current Log and type 1074 as the event ID.
If you double click the restart event you are interested in you should see the userid of the person who initiated the restart in the event properties window.or (via Powershell)...
Get-WinEvent -ComputerName localhost -FilterHashTable @{logname="System";id="1074"} -MaxEvents 10
06/10/2024 09:50:35 1074 Information The process C:\windows\system32\winlogon.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user MYDOMAIN\myuser for the following reason: No title for this reason could be found.
06/10/2024 09:50:27 1074 Information The process wusa.exe has initiated the restart of computer MYHOST on behalf of user MYDOMAIN\myuser for the following reason: Operating System: Recovery (Planned).
06/10/2024 07:34:52 1074 Information The process C:\windows\servicing\TrustedInstaller.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user NT AUTHORITY\SYSTEM for the following reason: Operating System: Upgrade (Planned).
01/09/2024 01:52:21 1074 Information The process C:\Program Files (x86)\ManageEngine\UEMS_Agent\bin\dcmsghandler.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user NT AUTHORITY\SYSTEM for the following reason: Legacy API shutdown.
01/09/2024 00:42:12 1074 Information The process C:\windows\servicing\TrustedInstaller.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user NT AUTHORITY\SYSTEM for the following reason: Operating System: Upgrade (Planned).
01/09/2024 00:18:52 1074 Information The process C:\windows\system32\shutdown.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user MYDOMAIN\myuser for the following reason: No title for this reason could be found.
29/08/2024 20:55:28 1074 Information The process C:\windows\system32\winlogon.exe (MYHOST) has initiated the restart of computer MYHOST on behalf of user MYDOMAIN\myuser for the following reason: No title for this reason could be found.
29/08/2024 20:55:25 1074 Information The process wusa.exe has initiated the restart of computer MYHOST on behalf of user MYDOMAIN\myuser for the following reason: Operating System: Recovery (Planned).
Other useful diagnostics
Get-WinEvent -ComputerName localhost -FilterHashtable @{logname = 'Application'; id = 17147} -MaxEvents 1
Get-WinEvent -ComputerName localhost -FilterHashtable @{logname = 'Application'; id = 17148} -MaxEvents 1
Get-WinEvent -ComputerName localhost -FilterHashtable @{logname = 'System'; id = 7036} -MaxEvents 2000 | Out-String -Stream | Select-String -pattern "SQL"
Bibliography
https://learn.microsoft.com/en-us/answers/questions/529355/event-id-for-sql-server-stopped-and-event-id-for-shttps://stackoverflow.com/questions/496632/is-it-possible-to-log-who-started-or-stopped-a-windows-service
Powershellhttps://stackoverflow.com/questions/64183006/how-to-use-select-string-to-filter-lines-from-an-outputhttps://stackoverflow.com/questions/56083896/powershell-script-to-filter-windows-event-logshttps://adamtheautomator.com/powershell-grep/https://gallery.technet.microsoft.com/scriptcenter/Get-Windows-Servers-last-a6edfe89