-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckSoftwareUpdates.vbs
More file actions
380 lines (322 loc) · 15.6 KB
/
CheckSoftwareUpdates.vbs
File metadata and controls
380 lines (322 loc) · 15.6 KB
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
dim WPURL 'Web page URL
dim WPData 'Web page text
dim WPQTH 'Update location
dim WPVar 'Location Varience
dim WPVerGuess 'After NewVersionGuess is called, this will store the version from the web page
dim OVersion 'Oldest Installed version
dim CVersion 'Installed version
dim outputl 'Email body
dim URLERR 'URLS with errors
dim j 'Counter
dim bofp 'Length of outputl before addition of new row. Used if we need to remove a false positive
set xmlhttp = createobject("msxml2.serverxmlhttp.3.0")
Dim adoconn
Dim rs
Dim str
set filesys=CreateObject("Scripting.FileSystemObject")
Dim strCurDir
strCurDir = filesys.GetParentFolderName(Wscript.ScriptFullName)
'Gather variables from smapp.ini
If filesys.FileExists(strCurDir & "\smapp.ini") then
'Database
DBLocation = ReadIni(strCurDir & "\smapp.ini", "Database", "DBLocation" )
DBUser = ReadIni(strCurDir & "\smapp.ini", "Database", "DBUser" )
DBPass = ReadIni(strCurDir & "\smapp.ini", "Database", "DBPass" )
'Email - Defaults to anonymous login
RptToEmail = ReadIni(strCurDir & "\smapp.ini", "Email", "RptToEmail" )
RptFromEmail = ReadIni(strCurDir & "\smapp.ini", "Email", "RptFromEmail" )
EmailSvr = ReadIni(strCurDir & "\smapp.ini", "Email", "EmailSvr" )
EmailPort = ReadIni(strCurDir & "\smapp.ini", "Email", "EmailPort" )
EmailAuthType = ReadIni(strCurDir & "\smapp.ini", "Email", "EmailAuthType" )
EmailUserName = ReadIni(strCurDir & "\smapp.ini", "Email", "EmailUserName" )
EmailPassword = ReadIni(strCurDir & "\smapp.ini", "Email", "EmailPassword" )
'Additional email settings found in smapp.ini
'WebGUI
BaseURL = ReadIni(strCurDir & "\smapp.ini", "WebGUI", "BaseURL" )
else
msgbox "INI file not found at: " & strCurDir & "\smapp.ini" & vbCrlf & "Please run IngestCSV.vbs first before running this file."
end if
outputl = "There is currently no out-of-date software."
URLERR = ""
j = 0
Set adoconn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
adoconn.Open "Driver={" & GetMySQLDriver & "};Server=" & DBLocation & ";" & _
"Database=software_matrix; User=" & DBUser & "; Password=" & DBPass & ";"
str = "Select * from discoveredapplications where ((not UpdateURL = '' and UpdateURL IS NOT NULL) or Version_Oldest <> Version_Newest) and LastDiscovered = (select max(LastDiscovered) from discoveredapplications) order by Name;"
rs.Open str, adoconn, 3, 3 'OpenType, LockType
if not rs.eof then rs.MoveFirst
do while not rs.eof
WPURL = rs("UpdateURL")
WPQTH = rs("UpdatePageQTH")
WPVar = rs("UpdatePageQTHVarience")
OVersion = rs("Version_Oldest")
CVersion = rs("Version_Newest")
if len(WPURL) > 0 then
'Pull website
On error resume next
xmlhttp.open "get", WPURL, false
xmlhttp.send
WPData = xmlhttp.responseText
if WPData = "" then URLERR = URLERR & WPURL & "<br>" & vbCrlf
'Check to see if exists
if instr(1,WPData,CVersion,0)>0 then
if instr(1,WPData,CVersion,0) => WPQTH + WPVar or instr(1,WPData,CVersion,0) =< WPQTH - WPVar then
if outputl = "There is currently no out-of-date software." then AddHeaderInfo 'Add Header Info
bofp = len(outputl)
outputl = outputl & "<tr>" & vbcrlf
outputl = outputl & " <td>" & rs("Name") & "</td>" & vbcrlf
outputl = outputl & " <td>" & OVersion & "</td>" & vbcrlf
outputl = outputl & " <td>" & CVersion & "</td>" & vbcrlf
WPVerGuess = NewVersionGuess(WPData, CVersion)
if WPVerGuess => WPQTH + WPVar or WPVerGuess =< WPQTH - WPVar then
if BaseURL = "" then 'No web GUI installed
if instr(1,WPData,CVersion,0) => WPQTH + WPVar then
outputl = outputl & " <td bgcolor=yellow>Possible Update (" & instr(1,WPData,CVersion,0) - WPQTH & ")</td>" & vbcrlf
elseif instr(1,WPData,CVersion,0) =< WPQTH - WPVar then
outputl = outputl & " <td bgcolor=yellow>Possible Update (" & WPQTH - instr(1,WPData,CVersion,0) & ")</td>" & vbcrlf
end if
else 'Web GUI, add links to fix descrepencies
if instr(1,WPData,CVersion,0) => WPQTH + WPVar then
outputl = outputl & " <td bgcolor=yellow><a style=""color:black"" href=""" & BaseURL & "/possibleupdate.php?id=" & rs("ID") & "&qth=" & instr(1,WPData,CVersion,0) & """>Possible Update (" & instr(1,WPData,CVersion,0) - WPQTH & ")</a></td>" & vbcrlf
elseif instr(1,WPData,CVersion,0) =< WPQTH - WPVar then
outputl = outputl & " <td bgcolor=yellow><a style=""color:black"" href=""" & BaseURL & "/possibleupdate.php?id=" & rs("ID") & "&qth=" & instr(1,WPData,CVersion,0) & """>Possible Update (" & WPQTH - instr(1,WPData,CVersion,0) & ")</a></td>" & vbcrlf
end if
end if
else
outputl = outputl & " <td bgcolor=red>New Version " & mid(WPData,WPVerGuess+1,len(CVersion)) & "</td>" & vbcrlf
end if
if mid(WPData,WPVerGuess+1,len(CVersion)) = CVersion then
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf
outputl = outputl & "</tr>" & vbcrlf
if WPVar > 0 then
if NewVersionGuess(replace(mid(WPData,instr(1,WPData,CVersion,0) - WPVar,(WPVar * 2)),CVersion,""), CVersion) = 0 then rs("UpdatePageQTHVarience") = WPVar * 2
else
rs("UpdatePageQTHVarience") = 10
end if
rs("UpdatePageQTH") = instr(1,WPData,CVersion,0)
'msgbox rs("Name") & " updated." & vbCrlf & "Location - Old: " & WPQTH & " New: " & rs("UpdatePageQTH") & vbCrlf & "Variance - Old: " & WPVar & " New: " & rs("UpdatePageQTHVarience")
outputl = left(outputl,bofp) 'Remove false positive
j = j - 1
rs.update
else
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf
outputl = outputl & "</tr>" & vbcrlf
end if
j = j + 1
elseif OVersion <> CVersion then
if outputl = "There is currently no out-of-date software." then AddHeaderInfo 'Add Header Info
outputl = outputl & "<tr>" & vbcrlf
outputl = outputl & " <td>" & rs("Name") & "</td>" & vbcrlf
outputl = outputl & " <td bgcolor=yellow>" & OVersion & "</td>" & vbcrlf
outputl = outputl & " <td>" & CVersion & "</td>" & vbcrlf
outputl = outputl & " <td bgcolor=green>Continue Updating</td>" & vbcrlf
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf 'Download
outputl = outputl & "</tr>" & vbcrlf
j = j + 1
end if
'msgbox len(WPQTH & "")
'msgbox instr(1,WPData,CVersion,0)
if len(WPQTH & "") = 0 or WPQTH = 0 then
rs("UpdatePageQTH") = instr(1,WPData,CVersion,0)
rs.update
end if
'msgbox rs("Name") & ": The installed version, " & CVersion & ", is the latest version."
else
if outputl = "There is currently no out-of-date software." then AddHeaderInfo 'Add Header Info
bofp = len(outputl)
outputl = outputl & "<tr>" & vbcrlf
outputl = outputl & " <td>" & rs("Name") & "</td>" & vbcrlf
outputl = outputl & " <td>" & OVersion & "</td>" & vbcrlf
outputl = outputl & " <td>" & CVersion & "</td>" & vbcrlf
WPVerGuess = NewVersionGuess(WPData, CVersion)
if WPVerGuess <> 0 then
outputl = outputl & " <td bgcolor=red>New Version " & mid(WPData,WPVerGuess+1,len(CVersion)) & "</td>" & vbcrlf
if int(replace(mid(WPData,WPVerGuess+1,len(CVersion)), ".", "")) > int(replace(CVersion, ".", "")) then
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf 'Download
outputl = outputl & "</tr>" & vbcrlf
else
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf 'Download
outputl = outputl & "</tr>" & vbcrlf
outputl = left(outputl,bofp) 'Remove false positive
j = j - 1
end if
else
outputl = outputl & " <td bgcolor=yellow>Verify URL (N/A)</td>" & vbcrlf
outputl = outputl & " <td><a href=""" & WPURL & """>Download</a></td>" & vbcrlf 'Download
outputl = outputl & "</tr>" & vbcrlf
end if
'msgbox rs("Name") & ": The installed version, " & CVersion & ", is not the latest version. Please download new version at: " & WPURL
j = j + 1
End if
else
if outputl = "There is currently no out-of-date software." then AddHeaderInfo 'Add Header Info
outputl = outputl & "<tr>" & vbcrlf
outputl = outputl & " <td>" & rs("Name") & "</td>" & vbcrlf
outputl = outputl & " <td bgcolor=yellow>" & OVersion & "</td>" & vbcrlf
outputl = outputl & " <td>" & CVersion & "</td>" & vbcrlf
outputl = outputl & " <td>Continue Updating</td>" & vbcrlf
outputl = outputl & " <td></td>" & vbcrlf 'Download
outputl = outputl & "</tr>" & vbcrlf
j = j + 1
end if
rs.MoveNext
loop
rs.close
'Clean up and output errors to bottom of email
outputl = outputl & "</table>"
if not URLERR = "" then outputl = outputl & "<br><br>URLs with Errors:<br>" & URLERR
'Add counter to top
outputl = replace(outputl, "%NEEDUPDATES%", j)
SendMail RptToEmail, "Software Matrix: Update Report"
Function SendMail(TextRcv,TextSubject)
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = TextSubject
objMessage.From = RptFromEmail
objMessage.To = TextRcv
objMessage.HTMLBody = outputl
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = EmailSvr
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = EmailAuthType
if EmailAuthType > 0 then
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = EmailUserName
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = EmailPassword
end if
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = EmailPort
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
End Function
Function ReadIni( myFilePath, mySection, myKey ) 'Thanks to http://www.robvanderwoude.com
' This function returns a value read from an INI file
'
' Arguments:
' myFilePath [string] the (path and) file name of the INI file
' mySection [string] the section in the INI file to be searched
' myKey [string] the key whose value is to be returned
'
' Returns:
' the [string] value for the specified key in the specified section
'
' CAVEAT: Will return a space if key exists but value is blank
'
' Written by Keith Lacelle
' Modified by Denis St-Pierre and Rob van der Woude
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim intEqualPos
Dim objFSO, objIniFile
Dim strFilePath, strKey, strLeftString, strLine, strSection
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
ReadIni = ""
strFilePath = Trim( myFilePath )
strSection = Trim( mySection )
strKey = Trim( myKey )
If objFSO.FileExists( strFilePath ) Then
Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
Do While objIniFile.AtEndOfStream = False
strLine = Trim( objIniFile.ReadLine )
' Check if section is found in the current line
If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
strLine = Trim( objIniFile.ReadLine )
' Parse lines until the next section is reached
Do While Left( strLine, 1 ) <> "["
' Find position of equal sign in the line
intEqualPos = InStr( 1, strLine, "=", 1 )
If intEqualPos > 0 Then
strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
' Check if item is found in the current line
If LCase( strLeftString ) = LCase( strKey ) Then
ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
' In case the item exists but value is blank
If ReadIni = "" Then
ReadIni = " "
End If
' Abort loop when item is found
Exit Do
End If
End If
' Abort if the end of the INI file is reached
If objIniFile.AtEndOfStream Then Exit Do
' Continue with next line
strLine = Trim( objIniFile.ReadLine )
Loop
Exit Do
End If
Loop
objIniFile.Close
Else
WScript.Echo strFilePath & " doesn't exists. Exiting..."
Wscript.Quit 1
End If
End Function
Function NewVersionGuess(byval Haystack, byval OldVerFind) 'Finds similar match (hopefully newer) to the current version based on pattern
dim VerPattern
VerPattern = ""
if len(OldVerFind) > 1 then
for i = 1 to len(OldVerFind)
if mid(OldVerFind,i,1)="." then
VerPattern = VerPattern & "\."
else
VerPattern = VerPattern & "\d"
end if
next
end if
Set re = New RegExp
re.Pattern = VerPattern
Set matches = re.Execute(Haystack)
If matches.Count > 0 Then
set match = matches(0)
NewVersionGuess = Match.FirstIndex
else
NewVersionGuess = 0
End If
End Function
Function AddHeaderInfo()
outputl = "<html><head> <style>BODY{font-family: Arial; font-size: 10pt;}TABLE{border: 1px solid black; border-collapse: collapse;}TH{border: 1px solid black; background: #dddddd; padding: 5px; }TD{border: 1px solid black; padding: 5px; }</style> </head><body>" & vbcrlf
outputl = outputl & "Updates needed: %NEEDUPDATES%<br><br>" & vbcrlf
outputl = outputl & "<table>" & vbcrlf
outputl = outputl & "<tr>" & vbcrlf
outputl = outputl & " <th>Application</th>" & vbcrlf
outputl = outputl & " <th>Oldest Version</th>" & vbcrlf
outputl = outputl & " <th>Newest Version</th>" & vbcrlf
outputl = outputl & " <th>Determination</th>" & vbcrlf
outputl = outputl & " <th>Update URL</th>" & vbcrlf
outputl = outputl & "</tr>" & vbcrlf
End Function
'Get installed MySQL driver name from HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\
function GetMySQLDriver()
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
if instr(1,subkey,"MySQL ODBC",1) >0 and instr(1,subkey,"ANSI Driver",1) then GetMySQLDriver = subkey
Next
end function