-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBootTime.ps1
234 lines (178 loc) · 7.64 KB
/
BootTime.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
$Version = " -- Version: 1.1.1"
# COMMON coding
CLS
# init flags
$StatusObj = [PSCustomObject] [ordered] @{Scripterror = $false;
Recordslogged = $false;
}
$InformationPreference = "Continue"
$WarningPreference = "Continue"
$ErrorActionPreference = "Stop"
# ------------------ FUNCTIONS
function Report ([string]$level, [string]$line, [object]$Obj, [string]$file ) {
switch ($level) {
("N") {$rptline = $line}
("I") {
$rptline = "Info *".Padright(10," ") + $line
}
("A") {
$rptline = "Caution *".Padright(10," ") + $line
}
("B") {
$rptline = " *".Padright(10," ") + $line
}
("C") {
$rptline = "Change *".Padright(10," ") + $line
}
("W") {
$rptline = "Warning *".Padright(10," ") + $line
}
("E") {
$rptline = "Error *".Padright(10," ") + $line
$obj.scripterror = $true
}
("G") {
$rptline = "GIT: *".Padright(10," ") + $line
}
default {
$rptline = "Error *".Padright(10," ") + "Messagelevel $level is not valid"
$Obj.Scripterror = $true
}
}
Add-Content $file $rptline
}
# ------------------------ END OF FUNCTIONS
# ------------------------ START OF MAIN CODE
$Node = " -- Node: " + $env:COMPUTERNAME
$myname = $MyInvocation.MyCommand.Name
$enqprocess = $myname.ToUpper().Replace(".PS1","")
$FullScriptName = $MyInvocation.MyCommand.Definition
$mypath = $FullScriptName.Replace($MyName, "")
$LocalInitVar = $mypath + "InitVar.PS1"
$InitObj = & "$LocalInitVar" "OBJECT"
if ($Initobj.Abend) {
# Write-Warning "YES"
throw "INIT script $LocalInitVar Failed"
}
# END OF COMMON CODING
$gp = Get-Process -id $pid
$ProcessID = $gp.Id
$ProcessName = $gp.Name
$separator = "-".PadRight(120,"-")
# Init reporting file
$dir = $ADHC_TempDirectory + $ADHC_BootTimeLog.Directory
New-Item -ItemType Directory -Force -Path $dir | Out-Null
$tempfile = $dir + $ADHC_BootTimeLog.Name
foreach ($entry in $InitObj.MessageList){
Report $entry.Level $entry.Message $StatusObj $tempfile
}
# Init jobstatus file
$dir = $ADHC_OutputDirectory + $ADHC_Jobstatus
New-Item -ItemType Directory -Force -Path $dir | Out-Null
$p = $myname.Split(".")
$process = $p[0]
$jobstatus = $ADHC_OutputDirectory + $ADHC_Jobstatus + $ADHC_Computer + "_" + $Process + ".jst"
$errorcount = 0
$loop = 0
$myname = $MyInvocation.MyCommand.Name
$FullScriptName = $MyInvocation.MyCommand.Definition
$mypath = $FullScriptName.Replace($MyName, "")
Set-Content $tempfile "Started... up time monitoring" -Force
foreach ($entry in $InitObj.MessageList) {
Report $entry.Level $entry.Message $StatusObj $Tempfile
}
do {
$d = Get-Date
$Datum = " -- Date: " + $d.ToString("dd-MM-yyyy")
$Tijd = " -- Time: " + $d.ToString("HH:mm:ss")
$loop = $loop + 1
Report "N" $Separator $StatusObj $Tempfile
$Scriptmsg = "*** STARTED *** " + $mypath + " -- PowerShell script " + $MyName + $Version + $Datum + $Tijd +$Node
Report "N" $Scriptmsg $StatusObj $Tempfile
Write-Information $Scriptmsg
Report "I" "Process name = $ProcessName, proces ID = $ProcessID" $StatusObj $Tempfile
Report "I" "Iteration number $loop" $StatusObj $Tempfile
try {
# get boottime of machine
Report "I" "Get boottime from machine $ADHC_Computer" $StatusObj $Tempfile
$bt = Get-CimInstance -Class Win32_OperatingSystem | Select-Object LastBootUpTime
$boottime = $bt.LastBootUpTime
# Init boottime file if not existent
$str = $ADHC_BootTime.Split("\")
$dir = $ADHC_OutputDirectory + $str[0]
New-Item -ItemType Directory -Force -Path $dir | Out-Null
$bootfile = $ADHC_OutputDirectory + $ADHC_BootTime
$lt = Test-Path $bootfile
if (!$lt) {
Set-Content $bootfile "$ADHC_Computer|01-01-2000 00:00:00|01-01-2000 00:00:00|0" -force
}
# Read bootfile
$bootrec = Get-Content $bootfile
if (!$bootrec) {
$bootrec = "$ADHC_Computer|01-01-2000 00:00:00|01-01-2000 00:00:00|0"
}
$stoptime = Get-Date # it's a minimal guess
$diff = NEW-TIMESPAN –Start $boottime –End $stoptime
# Only check job status if computer has been up for >1,5 hour
$uptime = [Math]::Round($diff.TotalMinutes, 1)
$ft = $boottime.ToString("dddd dd MMMM yyyy HH:mm:ss")
Report "I" "Boot date & time = $ft, Uptime = $uptime minutes" $StatusObj $Tempfile
$bootrec = "$ADHC_Computer" + "|" + $boottime.ToString("dd-MM-yyyy HH:mm:ss") + "|" + $stoptime.ToString("dd-MM-yyyy HH:mm:ss" + "|" + $uptime)
Set-Content $bootfile "$bootrec"
}
catch {
Report "E" "Error !!!" $StatusObj $Tempfile
$errorcount += 1
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$Dump = $_.Exception.ToSTring()
Report "E" "Message = $ErrorMessage" $StatusObj $Tempfile
Report "E" "Failed Item = $Faileditem" $StatusObj $Tempfile
Report "E" "Dump = $Dump" $StatusObj $Tempfile
}
finally {
if ($StatusObj.Scripterror) {
$dt = Get-Date
$jobline = $ADHC_Computer + "|" + $process + "|" + "9" + "|" + $version + "|" + $dt.ToString("dd-MM-yyyy HH:mm:ss")
Set-Content $jobstatus $jobline
Add-Content $jobstatus "Failed item = $FailedItem"
Add-Content $jobstatus "Errormessage = $ErrorMessage"
Add-Content $jobstatus "Dump info = $dump"
Report "E" "Failed item = $FailedItem" $StatusObj $Tempfile
Report "E" "Errormessage = $ErrorMessage" $StatusObj $Tempfile
Report "E" "Dump info = $dump" $StatusObj $Tempfile
}
else {
Report "I" "Script (iteration) ended normally $Datum $Tijd" $StatusObj $Tempfile
Report "N" " " $StatusObj $Tempfile
$dt = Get-Date
$jobline = $ADHC_Computer + "|" + $process + "|" + "0" + "|" + $version + "|" + $dt.ToString("dd-MM-yyyy HH:mm:ss")
Set-Content $jobstatus $jobline
}
Report "I" "Wait 300 seconds..." $StatusObj $Tempfile
Report "N" " " $StatusObj $Tempfile
try { # copy temp file
$deffile = $ADHC_OutputDirectory + $ADHC_BootTimeLog.Directory + $ADHC_BootTimeLog.Name
if ($loop -eq 1) {
$Copmov = & $ADHC_CopyMoveScript $TempFile $deffile "MOVE" "REPLACE" $TempFile
}
else {
$Copmov = & $ADHC_CopyMoveScript $TempFile $deffile "MOVE" "APPEND" $TempFile
}
}
Catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$Dump = $_.Exception.ToSTring()
$dt = Get-Date
$jobline = $ADHC_Computer + "|" + $process + "|" + "9" + "|" + $version + "|" + $dt.ToString("dd-MM-yyyy HH:mm:ss")
Set-Content $jobstatus $jobline
Add-Content $jobstatus "Failed item = $FailedItem"
Add-Content $jobstatus "Errormessage = $ErrorMessage"
Add-Content $jobstatus "Dump info = $Dump"
$errorcount += 1
}
Start-Sleep -s 300
}
} Until ($errorcount -gt 10)
Report "E" "Ended with error count = $errorcount" $StatusObj $Tempfile