-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIT_Reminder.vbs
457 lines (446 loc) · 18.4 KB
/
IT_Reminder.vbs
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
'File Name: IT_Reminder.vbs
'Version: v2.2, 11/25/2022
'Author: Justin Grimes, 7/16/2018
'--------------------------------------------------
'Supported Switches
' -t -Use with task scheduler to disable console output.
' -u -Use to display upcoming maintenence without updating cache or records.
' -s -Use to display due maintenance without updating cache or records.
'--------------------------------------------------
'--------------------------------------------------
'Global variable definitions used in this script.
Option Explicit
dim strComputer, param1, objFSO, objWMIService, arg, objNet, objShell, strComputerName, strUserName, timeStamp, objExcel, _
mailFile, logFile, cacheFile, checkArr(27,2), cacheDir, checkFile, checkFrequency, today, maxAge, strNotes, appDir, _
dFileMod, triggerEmail, writeFile, checkDataFile, dataFile, data, mFile, dataDir, maintDir, check, counter, _
checkFile1, checkFile2, appexcel, wb, maintFile, objWorkbook, r, stat, dateDue, companyAbbr, toEmail, fromEmail
'--------------------------------------------------
'--------------------------------------------------
'Define variables for the session.
counter = 0
strComputer = "."
param1 = checkDataFile = strNotes = dataFile = ""
appDir = "\\server\Scripts\IT_Reminder\"
cacheDir = appDir&"Cache"
dataDir = appDir&"Data"
maintDir = "\\server\Maintenance"
companyAbbr = "TCI"
toEmail = "HelpDesk@thecompany.com"
fromEmail = "HelpDesk@thecompany.com"
today = Date
timeStamp = Now
maxAge = 0
triggerEmail = FALSE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set arg = WScript.Arguments
Set objNet = CreateObject("Wscript.Network")
Set objShell = WScript.CreateObject("WScript.Shell")
strComputerName = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
strUserName = objShell.ExpandEnvironmentStrings("%USERNAME%")
mailFile = appDir&"Warning.mail"
'--------------------------------------------------
'--------------------------------------------------
'Define array elements for the session.
'This check is for keeping visibility on the logs directory.
'We build an array of checks to perform that will later be used to include .html files
'of the same names. The first array entry is the name of the check to be performed, and the
'secont entry is the frequency for the check. Supported frequencies are "DAILY", "WEEKLY",
'"MONTHLY", and "YEARLY"
'If you want to add or remove items from this array, you must also remember to modify the array indecies defined
'in the global variables section of this script.
'This check is for . . .
checkArr(0,0) = "DAILY_MAINT_ITEM_1"
checkArr(0,1) = "DAILY"
'This check is for . . .
checkArr(1,0) = "DAILY_MAINT_ITEM_2"
checkArr(1,1) = "DAILY"
'This check is for . . .
checkArr(2,0) = "DAILY_MAINT_ITEM_3"
checkArr(2,1) = "DAILY"
'This check is for . . .
checkArr(3,0) = "DAILY_MAINT_ITEM_4"
checkArr(3,1) = "DAILY"
'This check is for . . .
checkArr(4,0) = "TWODAYS_MAINT_ITEM_1"
checkArr(4,1) = "TWODAYS"
'This check is for . . .
checkArr(5,0) = "TWODAYS_MAINT_ITEM_2"
checkArr(5,1) = "TWODAYS"
'This check is for . . .
checkArr(6,0) = "TWODAYS_MAINT_ITEM_3"
checkArr(6,1) = "TWODAYS"
'This check is for . . .
checkArr(7,0) = "TWODAYS_MAINT_ITEM_4"
checkArr(7,1) = "TWODAYS"
'This check is for . . .
checkArr(8,0) = "THREEDAYS_MAINT_ITEM_1"
checkArr(8,1) = "THREEDAYS"
'This check is for . . .
checkArr(9,0) = "THREEDAYS_MAINT_ITEM_2"
checkArr(9,1) = "THREEDAYS"
'This check is for . . .
checkArr(10,0) = "THREEDAYS_MAINT_ITEM_3"
checkArr(10,1) = "THREEDAYS"
'This check is for . . .
checkArr(11,0) = "THREEDAYS_MAINT_ITEM_4"
checkArr(11,1) = "THREEDAYS"
'This check is for . . .
checkArr(12,0) = "WEEKLY_MAINT_ITEM_1"
checkArr(12,1) = "WEEKLY"
'This check is for . . .
checkArr(13,0) = "WEEKLY_MAINT_ITEM_2"
checkArr(13,1) = "WEEKLY"
'This check is for . . .
checkArr(14,0) = "WEEKLY_MAINT_ITEM_3"
checkArr(14,1) = "WEEKLY"
'This check is for . . .
checkArr(15,0) = "WEEKLY_MAINT_ITEM_4"
checkArr(15,1) = "WEEKLY"
'This check is for . . .
checkArr(16,0) = "MONTHLY_MAINT_ITEM_1"
checkArr(16,1) = "MONTHLY"
'This check is for . . .
checkArr(17,0) = "MONTHLY_MAINT_ITEM_2"
checkArr(17,1) = "MONTHLY"
'This check is for . . .
checkArr(18,0) = "MONTHLY_MAINT_ITEM_3"
checkArr(18,1) = "MONTHLY"
'This check is for . . .
checkArr(19,0) = "MONTHLY_MAINT_ITEM_4"
checkArr(19,1) = "MONTHLY"
'This check is for . . .
checkArr(20,0) = "MONTHLY_MAINT_ITEM_5"
checkArr(20,1) = "MONTHLY"
'This check is for . . .
checkArr(21,0) = "MONTHLY_MAINT_ITEM_6"
checkArr(21,1) = "MONTHLY"
'This check is for . . .
checkArr(22,0) = "MONTHLY_MAINT_ITEM_7"
checkArr(22,1) = "MONTHLY"
'This check is for . . .
checkArr(23,0) = "MONTHLY_MAINT_ITEM_8"
checkArr(23,1) = "MONTHLY"
'This check is for . . .
checkArr(24,0) = "MONTHLY_MAINT_ITEM_9"
checkArr(24,1) = "MONTHLY"
'This check is for . . .
checkArr(25,0) = "YEARLY_MAINT_ITEM_1"
checkArr(25,1) = "YEARLY"
'This check is for . . .
checkArr(26,0) = "YEARLY_MAINT_ITEM_2"
checkArr(26,1) = "YEARLY"
'--------------------------------------------------
'--------------------------------------------------
'A funciton for running SendMail.
Function SendEmail(appDir, mailFile)
objShell.run appDir&"sendmail.exe " & mailFile
End Function
'--------------------------------------------------
'--------------------------------------------------
'The following code checks for required directories and creates them if needed.
If Not objFSO.FolderExists(maintDir) Then
Set objFolder = objFSO.CreateFolder(maintDir)
End If
If Not objFSO.FolderExists(cacheDir) Then
Set objFolder = objFSO.CreateFolder(cachedir)
End If
If Not objFSO.FolderExists(dataDir) Then
Set objFolder = objFSO.CreateFolder(dataDir)
End If
'--------------------------------------------------
'--------------------------------------------------
'Retrieve the specified arguments.
If (arg.Count > 0) Then
param1 = arg(0)
End If
'--------------------------------------------------
'--------------------------------------------------
'The following code is performed when the -t argument is set (intended for scheduled tasks).
'When run as a task there are no outputs to the command line or dialouge boxes. Instead
'an email is sent to IT@company.com for every maintenance item that is due.
If (param1 = "-t") Then
For Each check In checkArr
If counter > 19 Then
Exit For
End If
If check = "" Then
Exit For
End If
triggerEmail = False
'The checkFile contains the maintenance log data.
'The checkDataFile contains the html formatted instructions for performing the maintenance.
checkFile = cacheDir & "\" & checkArr(counter,0) & ".txt"
checkDataFile = dataDir & "\" & checkArr(counter,0) & ".html"
checkFrequency = checkArr(counter,1)
'Create the cache file if it was expired, deleted, or missing.
If Not objFSO.FileExists(checkFile) Then
maxAge = -1
End If
'If the checkFile exists, get it's age.
If objFSO.FileExists(checkFile) Then
Set checkFile1 = objFSO.GetFile(checkFile)
dFileMod = FormatDateTime(checkFile1.DateLastModified, "2")
'Set the maxAge if the current check is to be performed daily.
If (checkFrequency = "DAILY") Then
maxAge = 1
'Set the maxAge if the current check is to be performed every 2 days.
If (checkFrequency = "TWODAYS") Then
maxAge = 2
'Set the maxAge if the current check is to be performed every 3 days.
If (checkFrequency = "THREEDAYS") Then
maxAge = 3
End If
'Set the maxAge if the current check is to be performed weekly.
If (checkFrequency = "WEEKLY") Then
maxAge = 7
End If
'Set the maxAge if the current check is to be performed monthly.
If (checkFrequency = "MONTHLY") Then
maxAge = 30
End If
'Set the maxAge if the current check is to be performed yearly.
If (checkFrequency = "YEARLY") Then
maxAge = 365
End If
End If
'See if the cache file has expired.
If DateDiff("d", dFileMod, today) >= maxAge Then
triggerEmail = TRUE
End If
'Send an email if the selected cache file was expired.
If (triggerEmail = TRUE) Then
Wscript.Sleep(1000)
Set dataFile = objFSO.OpenTextFile(checkDataFile, 1)
data = dataFile.ReadAll
dataFile.Close
Set mFile = objFSO.CreateTextFile(mailFile, TRUE, FALSE)
mFile.Write "To: " & toEmail & VBNewLine & "From: " & fromEmail & VBNewLine & "Subject: " & companyAbbr & " IT Reminder, " & _
checkArr(counter,0) & "!!!" & VBNewLine & data & VBNewLine & VBNewLine & _
"This reminder was generated by " & strComputerName & " and is run once per day." & _
VBNewLine & VBNewLine & "Script: ""IT_Reminder.vbs"""
mFile.Close
Wscript.Sleep(1000)
SendEmail appDir, mailFile
triggerEmail = FALSE
End If
counter = counter + 1
Next
End If
'--------------------------------------------------
'--------------------------------------------------
'The following code is performed when the -t argument is NOT set (intended for users to run).
'When run directly dialogue boxes will be displayed to prompt the user to input maintenance records.
If (param1 <> "-t") And (param1 <> "-u") And (param1 <> "-s") Then
MsgBox "This application will check for due network maintenance. When maintenance is due this application will provide " & _
"procedures and instructions for performing it. " & VBNewLine & VBNewLine & "This application also gathers information about " & _
"performed maintainance and stores it in the form of Maintenance Record spreadsheets to ensure NIST compliance.", 64, companyAbbr & " IT Reminders"
For Each check In checkArr
If counter > 18 Then
Exit For
End If
'The checkFile contains the maintenance log data.
'The checkDataFile contains the html formatted instructions for performing the maintenance.
checkFile = cacheDir & "\" & checkArr(counter,0) & ".txt"
checkDataFile = dataDir & "\" & checkArr(counter,0) & ".html"
maintFile = maintDir & "\" & checkArr(counter,0) & ".xlsx"
checkFrequency = checkArr(counter,1)
'If the checkFile exists, get it's age.
If objFSO.FileExists(checkFile) Then
Set checkFile1 = objFSO.GetFile(checkFile)
dFileMod = FormatDateTime(checkFile1.DateLastModified, "2")
'Set the maxAge if the current check is to be performed daily.
If (checkFrequency = "DAILY") Then
maxAge = 1
End If
'Set the maxAge if the current check is to be performed weekly.
If (checkFrequency = "WEEKLY") Then
maxAge = 7
End If
'Set the maxAge if the current check is to be performed monthly.
If (checkFrequency = "MONTHLY") Then
maxAge = 30
End If
'Set the maxAge if the current check is to be performed yearly.
If (checkFrequency = "YEARLY") Then
maxAge = 365
End If
End If
If Not objFSO.FileExists(checkFile) Then
maxAge = -1
End If
'See if the cache file has expired.
If DateDiff("d", dFileMod, today) >= maxAge Then
Set dataFile = objFSO.OpenTextFile(checkDataFile, 1)
data = dataFile.ReadAll
dataFile.Close
'Throw a dialogue box to gather user input for the notes column of the maintenance record spreadsheet.
MsgBox data, 64, checkArr(counter,0)
strNotes = InputBox(checkArr(counter,0) & VBNewLine & VBNewLine & "Please perform the specified " & _
"maintenance and enter your notes below." & VBNewLine & VBNewLine & "You MUST leave a note to mark this " & _
"maintanence complete." & VBNewLine, checkArr(counter,0))
If strNotes <> "" Then
'Recreate the cache file if it already exists.
If objFSO.FileExists(checkFile) Then
objFSO.Deletefile(checkFile)
Set writeFile = objFSO.CreateTextFile(checkFile)
writeFile.Close
End If
'Create the cache file if it was expired, deleted, or missing.
If Not objFSO.FileExists(checkFile) Then
Set writeFile = objFSO.CreateTextFile(checkFile)
writeFile.Close
End If
'Create the maintenance file using Excel if it was deleted, or missing.
If Not objFSO.FileExists(maintFile) Then
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Add()
objWorkbook.SaveAs(maintFile)
objExcel.Quit
End If
'If the maintenance file exists, append the users input to it.
If objFSO.FileExists(maintFile) Then
Set checkFile2 = objFSO.GetFile(maintFile)
dFileMod = FormatDateTime(checkFile2.DateLastModified, "2")
Set appexcel = WScript.CreateObject("Excel.Application")
With appexcel
.Visible = False
Set wb = .Workbooks.Open(maintFile)
r = 1
Do Until Len(.Cells(r, 1).Value) = 0
r = r + 1
Loop
.Cells(r, 1).Value = timeStamp
.Cells(r, 2).Value = strUserName
.Cells(r, 3).Value = strComputerName
.Cells(r, 4).Value = checkArr(counter,0)
.Cells(r, 5).Value = checkArr(counter,1)
.Cells(r, 6).Value = strNotes
wb.Save
Set wb = Nothing
.Quit
End With
Set appexcel = Nothing
End If
End If
End If
counter = counter + 1
Next
MsgBox "All maintenance complete!" & VBNewLine, 64, companyAbbr & " IT Reminders"
End If
'--------------------------------------------------
'--------------------------------------------------
' The following code is performed when the "-u" argument is set.
'The "-U" argument displays upcoming maintenance but does not update the cache or records.
If (param1 = "-u") Then
MsgBox "This application will check for upcoming network maintenance. When maintenance is due this application will provide " & _
"procedures and instructions for performing it. " & VBNewLine & VBNewLine & "This application also gathers information about " & _
"performed maintainance and stores it in the form of Maintenance Record spreadsheets to ensure NIST compliance.", 64, companyAbbr & " IT Reminders"
For Each check In checkArr
If counter > 18 Then
Exit For
End If
'The checkFile contains the maintenance log data.
checkFile = cacheDir & "\" & checkArr(counter,0) & ".txt"
checkFrequency = checkArr(counter,1)
'Create the cache file if it was expired, deleted, or missing.
If Not objFSO.FileExists(checkFile) Then
Set writeFile = objFSO.CreateTextFile(checkFile)
writeFile.Close
maxAge = -1
End If
'If the checkFile exists, get it's age.
If objFSO.FileExists(checkFile) Then
Set checkFile1 = objFSO.GetFile(checkFile)
dFileMod = FormatDateTime(checkFile1.DateLastModified, "2")
'Set the maxAge if the current check is to be performed daily.
If (checkFrequency = "DAILY") Then
maxAge = 1
End If
'Set the maxAge if the current check is to be performed weekly.
If (checkFrequency = "WEEKLY") Then
maxAge = 7
End If
'Set the maxAge if the current check is to be performed monthly.
If (checkFrequency = "MONTHLY") Then
maxAge = 30
End If
'Set the maxAge if the current check is to be performed yearly.
If (checkFrequency = "YEARLY") Then
maxAge = 365
End If
End If
'See when the cache files expire and adjust the display message accordingly.
If DateDiff("d", dFileMod, today) >= maxAge Then
stat = "Overdue"
If maxAge = -1 Then
dFileMod = "Never"
dateDue = "ASAP"
End If
If maxAge <> -1 Then
dateDue = DateAdd("d", maxAge, dFileMod)
End If
End If
If DateDiff("d", dFileMod, today) <= maxAge Then
stat = "Current"
dateDue = DateAdd("d", maxAge, dFileMod)
End If
MsgBox checkArr(counter,0) & " - " & checkArr(counter, 1) & VBNewLine & VBNewLine & "Interval: " & checkArr(counter, 1) & VBNewLine & _
"Status: " & stat & VBNewLine & "Last Completion Date: " & dFileMod & VBNewLine & "Next Due Date: " & dateDue & VBNewLine & VBNewLine, 64, companyAbbr & " IT Reminders"
counter = counter + 1
Next
End If
'--------------------------------------------------
'--------------------------------------------------
'The following code is performed when the -t argument is NOT set (intended for users to run).
'When run directly dialogue boxes will be displayed to prompt the user to input maintenance records.
If (param1 = "-s") Then
MsgBox "This application will check for due network maintenance. When maintenance is due this application will provide " & _
"procedures and instructions for performing it. ", 64, companyAbbr & " IT Reminders"
For Each check In checkArr
If counter > 18 Then
Exit For
End If
'The checkFile contains the maintenance log data.
'The checkDataFile contains the html formatted instructions for performing the maintenance.
checkFile = cacheDir & "\" & checkArr(counter,0) & ".txt"
checkDataFile = dataDir & "\" & checkArr(counter,0) & ".html"
maintFile = maintDir & "\" & checkArr(counter,0) & ".xlsx"
checkFrequency = checkArr(counter,1)
'If the checkFile exists, get it's age.
If objFSO.FileExists(checkFile) Then
Set checkFile1 = objFSO.GetFile(checkFile)
dFileMod = FormatDateTime(checkFile1.DateLastModified, "2")
'Set the maxAge if the current check is to be performed daily.
If (checkFrequency = "DAILY") Then
maxAge = 1
End If
'Set the maxAge if the current check is to be performed weekly.
If (checkFrequency = "WEEKLY") Then
maxAge = 7
End If
'Set the maxAge if the current check is to be performed monthly.
If (checkFrequency = "MONTHLY") Then
maxAge = 30
End If
'Set the maxAge if the current check is to be performed yearly.
If (checkFrequency = "YEARLY") Then
maxAge = 365
End If
End If
If Not objFSO.FileExists(checkFile) Then
maxAge = -1
End If
'See if the cache file has expired.
If DateDiff("d", dFileMod, today) >= maxAge Then
Set dataFile = objFSO.OpenTextFile(checkDataFile, 1)
data = dataFile.ReadAll
'Throw a dialogue box to gather user input for the notes column of the maintenance record spreadsheet.
MsgBox data, 64, checkArr(counter,0)
dataFile.Close
End If
counter = counter + 1
Next
MsgBox "Maintenance scan complete!" & VBNewLine & VBNewLine & "No more due maintenance items to display!" & VBNewLine, 64, companyAbbr & " IT Reminders"
End If
'--------------------------------------------------