-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInfrastructure_Checkup.vbs
346 lines (326 loc) · 13.9 KB
/
Infrastructure_Checkup.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
'File Name: Infrastructure_Checkup.vbs
'Version: v1.2, 11/25/2022
'Author: Justin Grimes, 3/5/2019
' --------------------------------------------------
'Declare explicit variables to be used during the session.
Option Explicit
Dim fileSystem, windowsVersion, oShell, computerName, mailData, mailFile, message, logPath, strSafeDate, strSafeTime, _
strDateTime, logfile, objLogFile, arguments, arg, args, error, appPath, verbose, email, logging, emailData, _
logData, dataDir, computerDir, OutputBox, processArch, sysArch, WshProcEnv, messageData, archType, checkupResults, _
requiredDir, computerDir0, computerDir1, argList, requiredDirs, requiredDirectories, mainError, dxDiagInfo, WshFinished, _
WshError, taskInfo, msInfo, diskInfo, companyName, companyAbbr, companyDomain, toEmail
' --------------------------------------------------
' --------------------------------------------------
'Set variables values for the session.
Set oShell = WScript.CreateObject("WScript.Shell")
Set arguments = WScript.Arguments
Set WshProcEnv = oShell.Environment("Process")
Set fileSystem = CreateObject("Scripting.FileSystemObject")
computerName = oShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
strSafeDate = DatePart("yyyy",Date)&Right("0"&DatePart("m",Date), 2)&Right("0"&DatePart("d",Date), 2)
strSafeTime = Right("0"&Hour(Now), 2)&Right("0"&Minute(Now), 2)&Right("0"&Second(Now), 2)
strDateTime = strSafeDate&"-"&strSafeTime
' ----------
' Company Specific variables.
' Change the following variables to match the details of your organization.
' The "appPath" is the full absolute path for the script directory, with trailing slash.
appPath = "\\server\Scripts\Infrastructure_Checkup\"
' The "logPath" is the full absolute path for where network-wide logs are stored.
logPath = "\\server\Logs"
' The "dataDir" is where all generated reports will be stored.
'Subdirectories for computer name and report time will be created in this directory.
dataDir = "\\server\Logs\Computers\"
' The "companyName" the the full, unabbreviated name of your organization.
companyName = "The Company Inc."
' The "companyAbbr" is the abbreviated name of your organization.
companyAbbr = "TCI"
' The "companyDomain" is the domain to use for sending emails. Generated report emails will appear
' to have been sent by "COMPUTERNAME@domain.com"
companyDomain = "thecompany.com"
' The "toEmail" is a valid email address where notifications will be sent.
toEmail = "helpdesk@thecompany.com"
' ----------
logfile = logPath&"\"&computerName&"-"&strDateTime&"-infrastructure_checkup.txt"
mailFile = appPath&"Warning.mail"
computerDir0 = dataDir&computerName
computerDir1 = computerDir0&"\Checkups"
computerDir = computerDir1&"\"&strSafeDate
requiredDirectories = Array(appPath, dataDir, computerDir0, computerDir1, computerDir, logPath)
mainError = False
' --------------------------------------------------
' --------------------------------------------------
'Retrieve the specified arguments.
'Supported arguments are:
' -v - Verbose operation. Output any messages to a MsgBox.
' -e - Email operation. Output any messages to an email.
' -l - Log operation. Output any messages to a logfile.
' -s - Do not output any messages to a MsgBox.
'Returns an array of arguments in the order listed below.
Function ParseArgs(args)
Dim outputArray(3)
error = verbose = email = logging = False
outputArray(0) = outputArray(1) = outputArray(2) = outputArray(3) = False
For Each arg In args
'Verbose
If arg = "-v" Then
outputArray(0) = True
End If
'Email
If arg = "-e" Then
outputArray(1) = True
End If
'Logging
If arg = "-l" Then
outputArray(2) = True
End If
'Silent mode (enable email+logging, no verbosity)
If arg = "-s" Then
outputArray(0) = False
outputArray(1) = True
outputArray(2) = True
outputArray(3) = True
End If
Next
If outputArray(0) = False And outputArray(1) = False And outputArray(2) = False And outputArray(3) = False Then
error = True
ParseArgs = False
End If
If error = -1 Then
ParseArgs = outputArray
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
' Verify that all required directories exist.
Function VerifyInstallation(requiredDirs)
error = VerifyInstallation = False
If IsArray(requiredDirs) Then
For Each requiredDir In requiredDirs
On Error Resume Next
'Verify the supplied directory.
If Not fileSystem.FolderExists(requiredDir) Then
fileSystem.CreateFolder(requiredDir)
If Not fileSystem.FolderExists(requiredDir) Then
error = True
End If
End If
Next
If error = -1 Then
VerifyInstallation = True
End If
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to send the notification email when -e is set.
'Returns "True" if email sent sucessfully, "False" on failure.
Function SendEmail(mailFile, mailContent)
error = True
If fileSystem.FileExists(mailFile) Then
fileSystem.DeleteFile(mailFile)
End If
If Not fileSystem.FileExists(mailFile) Then
Set mailData = fileSystem.CreateTextFile(mailFile, True, True)
mailData.Write mailContent
mailData.Close
End If
If fileSystem.FileExists(mailFile) Then
error = False
oShell.exec appPath&"sendmail.exe "&mailFile
End If
SendEmail = error
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to create a log file when -l is set.
'Returns "True" if logfile exists, "False" on error.
Function CreateLog(logFile, message)
error = True
If message <> "" Then
Set objLogFile = fileSystem.CreateTextFile(logFile, True)
objLogFile.WriteLine(message)
objLogFile.Close
End If
If fileSystem.FileExists(logFile) Then
error = False
End If
CreateLog = error
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to create a MsgBox when -v is set.
'Returns "True" if a MsgBox was displayed, "False" on error.
Function OutputMessage(message)
error = True
If message <> "" Then
error = False
OutputBox = MsgBox(message, 64, "Infrastructure Checkup")
End If
OutputMessage = error
End Function
' --------------------------------------------------
' --------------------------------------------------
'Determine the local system architecture so we don't encounter errors when we run tests.
'Returns either "x86" for 32 bit systems or "AMD64" for 64 bit systems.
Function DetermineArch()
processArch = sysArch = DetermineArch = False
processArch = WshProcEnv("PROCESSOR_ARCHITECTURE")
If processArch = "x86" Then
sysArch = WshProcEnv("PROCESSOR_ARCHITEW6432")
If sysArch = "" Then
sysArch = "x86"
End if
Else
sysArch = processArch
End If
If sysArch = "x86" Or sysArch = "AMD64" Then
DetermineArch = sysArch
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to retrieve and store the Windows version.
'Uses the Windows "ver" command.
Function GetWindowsVersion(computerDir, strDateTime)
Dim winVersionCacheFile, windowsVersion, winVersionCache, versionInfo
windowsVersion = GetWindowsVersion = False
versionInfo = ""
winVersionCacheFile = computerDir&"\Windows_Version_"&strDateTime&".txt"
Set windowsVersion = oShell.exec("wmic os get Caption,CSDVersion /value")
Select Case windowsVersion.Status
Case WshFinished
versionInfo = Trim(windowsVersion.StdOut.ReadAll)
End Select
Set winVersionCache = fileSystem.CreateTextFile(winVersionCacheFile, True, False)
winVersionCache.WriteLine(versionInfo)
winVersionCache.Close
If fileSystem.FileExists(winVersionCacheFile) Then
GetWindowsVersion = versionInfo
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to retrieve and store the DXDiag information.
'Uses the Windows "dxdiag" command.
Function GetDXInfo(archType, computerDir, strDateTime)
Dim dxCacheFile, dxInfo, dxCache, extraQuery
If archType = "AMD64" Then
extraQuery = " /64bit"
End If
dxInfo = GetDXInfo = False
dxCacheFile = computerDir&"\DXDiag_"&strDateTime&".txt"
Set dxInfo = oShell.exec("dxdiag"&extraQuery&" /t "&dxCacheFile)
If fileSystem.FileExists(dxCacheFile) Then
GetDXInfo = dxInfo
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to retrieve and store running task information.
'Uses the Windows "tasklist" command.
Function GetTaskInfo(computerDir, strDateTime)
Dim taskCacheFile, taskInfo, taskCache
taskInfo = GetTaskInfo = False
taskCacheFile = computerDir&"\Running_Tasks_"&strDateTime&".txt"
Set taskInfo = oShell.exec("tasklist /v")
Select Case taskInfo.Status
Case WshFinished
taskInfo = Trim(taskInfo.StdOut.ReadAll)
End Select
Set taskCache = fileSystem.CreateTextFile(taskCacheFile, True, False)
taskCache.WriteLine(taskInfo)
taskCache.Close
If fileSystem.FileExists(taskCacheFile) Then
GetTaskInfo = taskInfo
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to retrieve and store msinfo32 information.
'Uses the Windows "msinfo32" command.
Function GetMSInfo(computerDir, strDateTime)
Dim msCacheFile, msInfo, msCache
msInfo = GetMSInfo = False
msCacheFile = computerDir&"\MSInfo32_"&strDateTime&".txt"
Set msInfo = oShell.exec("msinfo32 /report """&msCacheFile&"""")
Select Case msInfo.Status
Case WshFinished
msInfo = Trim(msInfo.StdOut.ReadAll)
End Select
If fileSystem.FileExists(msCacheFile) Then
GetMSInfo = msInfo
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'A function to retrieve and store disk information.
'Uses the Windows "df" command.
Function GetDiskInfo(computerDir, strDateTime)
Dim diskCacheFile, diskInfo, diskCache
diskInfo = GetDiskInfo = False
diskCacheFile = computerDir&"\Disk_Info_"&strDateTime&".txt"
Set diskInfo = oShell.exec("fsutil volume diskfree C:")
Select Case diskInfo.Status
Case WshFinished
diskInfo = Trim(diskInfo.StdOut.ReadAll)
End Select
Set diskCache = fileSystem.CreateTextFile(diskCacheFile, True, False)
diskCache.WriteLine(diskInfo)
diskCache.Close
If fileSystem.FileExists(diskCacheFile) Then
GetDiskInfo = diskInfo
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'Perform checkup routine.
Function PerformCheckup(archType, verbose, email, logging, computerDir, strDateTime)
If (fileSystem.FolderExists(computerDir)) Then
windowsVersion = GetWindowsVersion(computerDir, strDateTime)
dxDiagInfo = GetDXInfo(archType, computerDir, strDateTime)
taskInfo = GetTaskInfo(computerDir, strDateTime)
msInfo = GetMSInfo(computerDir, strDateTime)
diskInfo = GetDiskInfo(computerDir, strDateTime)
'Check if all reports were genereated sucessfully.
If vartype(windowsVersion) <> 8 Or vartype(dxDiagInfo) = 8 Or vartype(taskInfo) = 8 Or vartype(msInfo) = 8 Or vartype(diskInfo) = 8 Then
mainError = True
End If
End If
End Function
' --------------------------------------------------
' --------------------------------------------------
'The main logic of the program.
'Start by parsing the supplied arguments.
argList = ParseArgs(arguments)
If IsArray(argList) Then
verbose = argList(0)
email = argList(1)
logging = argList(2)
'Verify that all required directories exist and the application is safe to run.
If VerifyInstallation(requiredDirectories) = -1 Then
archType = DetermineArch()
'Make sure we can determine the architecture of the target system before executing anything.
If archType = "x86" Or archType = "AMD64" Then
checkupResults = PerformCheckup(archType, verbose, email, logging, computerDir, strDateTime)
If verbose Then
messageData = "This is a notification from the " & companyName & " Network to inform you that the device "&computerName&" has completed a checkup. The results of the checkup are located in "&computerDir&"."& _
vbNewLine&vbNewLine&"Please log-in and verify the generated reports for "&computerName&"."
Call OutputMessage(messageData)
End If
If email = -1 Then
emailData = "To: " & toEmail & ""&vbNewLine&"From: "&computerName&"@" & companyDomain & ""&vbNewLine&"Subject: " & companyAbbr & " Infrastructure Checkup!!"&vbNewLine& _
"This is an automatic email from the " & companyName & " Network to notify you that the device "&computerName&" has completed a checkup. The results of the checkup are located in "&computerDir&"."& _
vbNewLine&vbNewLine&"Please log-in and verify the generated reports for "&computerName&"."&vbNewLine&vbNewLine& _
"This check was generated by "&computerName&" and is performed as a monthly scheduled task."&vbNewLine&vbNewLine&"Script: ""Infrastructure_Checkup.vbs"""
Call SendEmail(mailFile, emailData)
End If
If logging = -1 Then
logData = "This is an automatic message from the " & companyName & " Network to notify you that the device "&computerName&" has completed a checkup. The results of the checkup are located in "&computerDir&"."& _
vbNewLine&vbNewLine&"Please log-in and verify the generated reports for "&computerName&"."&vbNewLine&vbNewLine& _
"This check was generated by "&computerName&" and is performed as a monthly scheduled task."&vbNewLine&vbNewLine&"Script: ""Infrastructure_Checkup.vbs"""
Call CreateLog(logFile, logData)
End If
End If
End If
End If
' --------------------------------------------------