-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServerLastBootv2.ps1
263 lines (226 loc) · 8.49 KB
/
ServerLastBootv2.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<#
.SYNOPSIS
This script will send out an email when run.
.DESCRIPTION
Use this script to monitor system reboots and server downtime. Create a schedule task that runs at system start and
have it execute this script.
.NOTES
Requires PowerShell 4
Author: Clint McGuire
Source: http://www.clintmcguire.com
Version: 3
Date: 2014-01-31
Latest Update: 2016-05-02
HTML Functions borrowed from:
XenAppServerHealthCheck
Jason Poyner, [email protected], techblog.deptive.co.nz
.LINK
Here are some links that I found useful when writing this script.
https://social.technet.microsoft.com/wiki/contents/articles/17889.powershell-script-for-shutdownreboot-events-tracker.aspx
https://blogs.technet.com/b/heyscriptingguy/archive/2013/03/27/powertip-get-the-last-boot-time-with-powershell.aspx
http://blogs.warwick.ac.uk/markglover/entry/how_to_export/
https://blogs.technet.com/b/heyscriptingguy/archive/2011/12/08/read-a-csv-file-and-build-distinguished-names-on-the-fly-by-using-powershell.aspx
.EXAMPLES
#>
#==============================================================================================
### Variables
#==============================================================================================
# E-mail report details
$emailFrom = ''
$emailTo = ''
#$emailSubject = ("$ServerName Reboot - " + (Get-Date -format R))
$smtpServer = ''
$smptPort = ''
$currentDir = Get-Location
$ResultFile = Join-Path $CurrentDir.path ('Results.csv')
$ResultsHTM = Join-Path $currentDir.Path ('Reboots.htm')
$headerNames = 'Boot', 'Outage', 'ShutdownType'
$headerWidths = '6', '6', '6'
#==============================================================================================
### Functions
#==============================================================================================
Function Write-HtmlHeader
{
param($title, $fileName)
$date = ( Get-Date -format R)
$head = @"
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title>$title</title>
<STYLE TYPE="text/css">
<!--
td {
font-family: Tahoma;
font-size: 11px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
overflow: hidden;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;
table {
table-layout:fixed;
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='1200'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='48' align='center' valign="middle">
<font face='tahoma' color='#003399' size='4'>
<strong>$title - $date</strong></font>
</td>
</tr>
</table>
"@
$head | Out-File $fileName
}
# ==============================================================================================
Function Write-TableHeader
{
param($fileName)
$tableHeader = @"
<table width='1200'><tbody>
<tr bgcolor=#CCCCCC>
<td width='6%' align='center'><strong>Shutdown</strong></td>
"@
$i = 0
while ($i -lt $headerNames.count) {
$headerName = $headerNames[$i]
$headerWidth = $headerWidths[$i]
$tableHeader += "<td width='" + $headerWidth + "%' align='center'><strong>$headerName</strong></td>"
$i++
}
$tableHeader += '</tr>'
$tableHeader | Out-File $fileName -append
}
# ==============================================================================================
Function Write-Data #This function is not used because it doesn't handle the variable passed to it correctly for this implementation
{
param($data, $fileName)
$bgcolor = '#CCCCCC'
$fontColor = '#003399'
for($i=0;$i-le $Data.length-1;$i++){
$tableEntry += '<tr>'
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Shutdown) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Boot) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Outage) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].ShutdownType) </font></td>")
#$i++
$tableEntry += '</tr>'
}
$tableEntry | Out-File $fileName -append
}
# ==============================================================================================
Function Write-HtmlFooter
{
param($fileName)
@"
</table>
</body>
</html>
"@ | Out-File $FileName -append
}
#==============================================================================================
### Main Script
#==============================================================================================
$Expected = New-Object PSObject | Select-Object Date, User, Action, Process, Reason, ReasonCode, Comment
$Unexpected = New-Object PSObject | Select-Object Date, User, Action, Process, Reason, ReasonCode, Comment
$ServerName = (Get-CimInstance -ClassName win32_operatingsystem).csname
$StartTime = (Get-Date).AddDays(-14)
$AllResults = @()
#Check Event log for last clean shutdown/reboot
Get-WinEvent -FilterHashtable @{logname='System'; id=1074; StartTime=$StartTime} -MaxEvents 1 -ErrorAction SilentlyContinue |
ForEach-Object {
$Expected.Date = $_.TimeCreated
$Expected.User = $_.Properties[6].Value
$Expected.Process = $_.Properties[0].Value
$Expected.Action = $_.Properties[4].Value
$Expected.Reason = $_.Properties[2].Value
$Expected.ReasonCode = $_.Properties[3].Value
$Expected.Comment = $_.Properties[5].Value
}
#Check Event log for last unexpected shutdown
$Unexpected.Date = (Get-Date).AddDays(-365)
Get-WinEvent -FilterHashtable @{logname='System'; id=6008; StartTime=$StartTime} -MaxEvents 1 -erroraction silentlycontinue|
ForEach-Object {
#$Unexpected.Date = $_.TimeCreated
$UEDateRAW = $_.Properties[1]
$UEDay = $UEDateRAW.Value
$UEArray = $UEDay.ToCharArray()
for($i=0;$i-le $UEArray.length-1;$i++)
{
if (([int]$UEArray[$i] -le 57))
{
$UEDate += $array[$i]
}
}
$UETime = $_.Properties[0]
$UETimeArray = $UETime.Value
$DateTime = $UEDate + ' ' + $UETimeArray
$Unexpected.Date = [DateTime]$DateTime
$Unexpected.Action = 'Unexpected'
$Unexpected.Action = 'Unexpected'
}
#Determine if last shutdown was clean or unexpected and use most recent
If ($Expected.date -gt $Unexpected.date)
{
$LastDown = $Expected.Date
$Action = $Expected.Action
}
Else
{
$LastDown = $Unexpected.date
$Action = $Unexpected.Action
}
#Determine last boot time
$CIM = Get-CimInstance -ClassName win32_operatingsystem
$LastUp = $CIM.lastbootuptime
#Write details to CSV file
$Outage = $LastUp - $LastDown
$OutageTime = '{0:N2}' -f $Outage.TotalMinutes
$Results = New-Object PSObject
$Results | add-member -membertype NoteProperty -name 'Shutdown' -Value $LastDown
$Results | add-member -membertype NoteProperty -name 'Boot' -Value $LastUp
$Results | add-member -membertype NoteProperty -name 'Outage' -Value $OutageTime
$Results | add-member -membertype NoteProperty -name 'ShutdownType' -Value $Action
$Results | Export-CSV $ResultFile -notype -append
#Import CSV for comparison and reporting
$Data = Import-Csv $ResultFile
#Create HTML Report
Write-HtmlHeader 'Server Reboot Report' $resultsHTM
Write-TableHeader $resultsHTM
#Write Data
$bgcolor = '#CCCCCC'
$fontColor = '#003399'
for($i=0;$i-le $Data.length-1;$i++){
$tableEntry += '<tr>'
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Shutdown) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Boot) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].Outage) </font></td>")
$tableEntry += ("<td bgcolor='" + $bgcolor + "' align=center><font color='" + $fontColor + "'> $($Data[$i].ShutdownType) </font></td>")
$tableEntry += '</tr>'
}
$tableEntry | Out-File $resultsHTM -append
Write-HtmlFooter $resultsHTM
$emailSubject = ("$ServerName Reboot - " + (Get-Date -format R))
$mailMessageParameters = @{
From = $emailFrom
To = $emailTo
Subject = $emailSubject
SmtpServer = $smtpServer
Body = (Get-Content $resultsHTM) | Out-String
}
Send-MailMessage @mailMessageParameters -BodyAsHtml