-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-RebootHistory.ps1
88 lines (68 loc) · 2.42 KB
/
Get-RebootHistory.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Function Get-RebootHistory {
<#
.SYNOPSIS
This will output who initiated a reboot or shutdown event.
.NOTES
Name: Get-RebootHistory
Author: theSysadminChannel
Version: 1.0
DateCreated: 2020-Aug-5
.LINK
https://thesysadminchannel.com/get-reboot-history-using-powershell -
.EXAMPLE
Get-RebootHistory -ComputerName Server01, Server02
.EXAMPLE
Get-RebootHistory -DaysFromToday 30 -MaxEvents 1
.PARAMETER ComputerName
Specify a computer name you would like to check. The default is the local computer
.PARAMETER DaysFromToday
Specify the amount of days in the past you would like to search for
.PARAMETER MaxEvents
Specify the number of events you would like to search for (from newest to oldest)
#>
[CmdletBinding()]
param(
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[string[]] $ComputerName = $env:COMPUTERNAME,
[int] $DaysFromToday = 7,
[int] $MaxEvents = 9999
)
BEGIN {}
PROCESS {
foreach ($Computer in $ComputerName) {
try {
$Computer = $Computer.ToUpper()
$EventList = Get-WinEvent -ComputerName $Computer -FilterHashtable @{
Logname = 'system'
Id = '1074', '6008'
StartTime = (Get-Date).AddDays(-$DaysFromToday)
} -MaxEvents $MaxEvents -ErrorAction Stop
foreach ($Event in $EventList) {
if ($Event.Id -eq 1074) {
[PSCustomObject]@{
TimeStamp = $Event.TimeCreated
ComputerName = $Computer
UserName = $Event.Properties.value[6]
ShutdownType = $Event.Properties.value[4]
}
}
if ($Event.Id -eq 6008) {
[PSCustomObject]@{
TimeStamp = $Event.TimeCreated
ComputerName = $Computer
UserName = $null
ShutdownType = 'unexpected shutdown'
}
}
}
} catch {
Write-Error $_.Exception.Message
}
}
}
END {}
}