-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGet_All_Boot_times_from_local_computer_Make_Chart.ps1
152 lines (108 loc) · 5.49 KB
/
Get_All_Boot_times_from_local_computer_Make_Chart.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
# Get all boot time entries from local system and show Graph
# Works also with Windows 7 Powershell 2.0
# Run this script with Administrator privileges. Otherwise script can't read Event Log entries
#
function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
# If user is NOT Administrator
if (-not (Test-Administrator)) {
$OriginalColor = $host.ui.RawUI.ForegroundColor
$host.ui.RawUI.ForegroundColor = "Red"
Write-Output "`nYou need Administrator privileges to read Event Logs!"
Write-Output "Run this script with Run As Administrator`n"
$host.ui.RawUI.ForegroundColor = $OriginalColor
Exit 0
}
# Windows 7 Powershell 2.0 compatibility
#$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
# load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
#$startTime = Get-Date -Day ((Get-Date).AddDays(-180).Day) -Hour 0 -Minute 0 -Second 0
#$endTime = Get-Date -Hour 0 -Minute 0 -Second 0
$boottimes = @{}
# With Powershell 3.0+ this would be done
#$boottimes = [ordered]@{}
# create chart object
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 750
$Chart.Height = 400
$Chart.Left = 20
$Chart.Top = 20
# create a chartarea to draw on and add to chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$Chartarea.AxisY.Title = "Seconds"
$Chartarea.AxisX.Title = "Date"
$Chart.ChartAreas.Add($ChartArea)
[void]$Chart.Series.Add("MainPathBootTime")
$Chart.Series["MainPathBootTime"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line
[void]$Chart.Series.Add("BootPostBootTime")
$Chart.Series["BootPostBootTime"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line
# legend
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = "Legend1"
$Chart.Legends.Add($legend)
# Get OS Install Date
$ComputerInstallDate = ([WMI] "").ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate).ToString("yyyy-MM-dd")
Write-Output "Computer installed: $($ComputerInstallDate)"
[void]$Chart.Titles.Add("$($env:COMPUTERNAME) Boot Times (OS InstallDate $ComputerInstallDate)")
$Chart.Titles[0].Font = "Arial,13pt"
$Chart.Titles[0].Alignment = "topCenter"
$eventList = Get-WinEvent -FilterHashtable @{logname = "Microsoft-Windows-Diagnostics-Performance/Operational"; id = 100}
$DateValues = @()
$BootTimeValues = @()
$PostBootTimeValues = @()
foreach ($event in $eventList) {
$eventXML = [xml]$event.ToXml()
$bootstart = $eventXML.SelectSingleNode("//*[@Name='BootStartTime']")."#text"
$bootend = $eventXML.SelectSingleNode("//*[@Name='BootEndTime']")."#text"
$boottime = $eventXML.SelectSingleNode("//*[@Name='BootTime']")."#text"
$mainboottime = $eventXML.SelectSingleNode("//*[@Name='MainPathBootTime']")."#text"
$postboottime = $eventXML.SelectSingleNode("//*[@Name='BootPostBootTime']")."#text"
$pos = $bootstart.IndexOf("T")
$bootstartDate = $bootstart.Substring(0, $pos)
$bootstartTime = $bootstart.Substring($pos + 1)
$pos = $bootstartTime.IndexOf(".")
$bootstartTime = $bootstartTime.Substring(0, $pos)
$bootstart = $bootstart.replace('T', '_')
$pos = $bootend.IndexOf("T")
$bootendDate = $bootend.Substring(0, $pos)
$bootendTime = $bootend.Substring($pos + 1)
$pos = $bootendTime.IndexOf(".")
$bootendTime = $bootendTime.Substring(0, $pos)
$bootend = $bootend.replace('T', '_')
# Change date/time to Finnish format (sorry :) day.month.year
$day = $bootstart.Tostring().SubString(8, 2)
$month = $bootstart.Tostring().SubString(5, 2)
$year = $bootstart.Tostring().SubString(0, 4)
$bootStartTimeDDMMYYY = "$($day).$($month).$($year)"
$boottime = [Math]::Truncate($boottime / 1000)
$mainboottime = [Math]::Truncate($mainboottime / 1000)
$postboottime = [Math]::Truncate($postboottime / 1000)
$boottimes.add($bootstart, $mainboottime)
$DateValues += $bootStartTimeDDMMYYY
$BootTimeValues += $mainboottime
$PostBootTimeValues += $postboottime
Write-Output "$($env:COMPUTERNAME), Date:$($bootStartTimeDDMMYYY), BootStart:$($bootstartTime), BootEnd:$($bootendTime), MainBootTime:$($mainboottime), PostBootTime:$($postboottime), BootTime:$($boottime)"
$bootcount = ""; $bootstart = ""; $bootend = ""; $boottime = ""; $mainboottime = ""; $postboottime = "";
}
# Reverse values on array. Otherwise graph will show values in wrong order
# Reason is Windows 7 Powershell 2.0 compatibility
[array]::Reverse($DateValues)
[array]::Reverse($BootTimeValues)
[array]::Reverse($PostBootTimeValues)
$Chart.Series["MainPathBootTime"].Points.DataBindXY($DateValues, $BootTimeValues)
$Chart.Series["BootPostBootTime"].Points.DataBindXY($DateValues, $PostBootTimeValues)
# display the chart on a form
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text = "PowerShell Chart"
$Form.Width = 800
$Form.Height = 500
$Form.controls.add($Chart)
$Form.Add_Shown( {$Form.Activate()})
$Form.ShowDialog()