Created
February 15, 2025 10:35
-
-
Save filipnet/ad2c98ebe9d33a8cd3f5bf4f6314ef06 to your computer and use it in GitHub Desktop.
PowerShell script saved as Get-RebootEvents.ps1 that retrieves system shutdown and reboot events for both single and multiple servers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Tracks system shutdown and reboot events. | |
| .DESCRIPTION | |
| This script retrieves event ID 1074 from the System log to identify shutdown and reboot events. | |
| It can generate reports for a single server or multiple servers. | |
| .PARAMETER Computer | |
| The target computer name (default: local computer). | |
| .PARAMETER ComputerList | |
| A file containing a list of remote computers (one per line). | |
| .EXAMPLE | |
| .\Get-RebootEvents.ps1 -Computer "PC123" | |
| Retrieves reboot events for PC123. | |
| .EXAMPLE | |
| .\Get-RebootEvents.ps1 -ComputerList "computers.txt" | |
| Retrieves reboot events for multiple servers listed in `computers.txt`. | |
| .NOTES | |
| - Requires administrative privileges. | |
| - Ensure WinRM is enabled for remote queries. | |
| - Event ID 1074 logs shutdown/reboot events. | |
| #> | |
| param ( | |
| [string]$Computer = $env:COMPUTERNAME, | |
| [string]$ComputerList | |
| ) | |
| function Get-RebootReport { | |
| param ( | |
| [string]$TargetComputer | |
| ) | |
| Write-Host "Generating reboot report for: $TargetComputer" -ForegroundColor Cyan | |
| try { | |
| Get-WinEvent -ComputerName $TargetComputer -FilterHashtable @{LogName='System'; ID=1074} | | |
| ForEach-Object { | |
| [PSCustomObject]@{ | |
| Date = $_.TimeCreated | |
| User = $_.Properties[6].Value | |
| Action = $_.Properties[4].Value | |
| Reason = $_.Properties[2].Value | |
| } | |
| } | Format-Table -AutoSize | |
| } | |
| catch { | |
| Write-Host "Error retrieving events from $TargetComputer: $_" -ForegroundColor Red | |
| } | |
| } | |
| if ($ComputerList) { | |
| if (Test-Path $ComputerList) { | |
| $Servers = Get-Content $ComputerList | |
| foreach ($Server in $Servers) { | |
| Get-RebootReport -TargetComputer $Server | |
| } | |
| } | |
| else { | |
| Write-Host "File $ComputerList not found!" -ForegroundColor Red | |
| } | |
| } | |
| else { | |
| Get-RebootReport -TargetComputer $Computer | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment