VBScript to Check for Windows Updates

Sometimes it is handy to receive an email update from servers that require updates. Here is a script that you can use to check for updates on a server and email you when updates are needed.

 

This requires two scripts and a commandline email helper. I use blat, but there are many other options. I call this first script updates.cmd.

 

@echo off

rem clear out the updates file and throw in a quick timestamp
del updates.txt
time /t >> updates.txt
date /t >> updates.txt

rem run the updates vbscript
cscript //nologo updates.vbs >> updates.txt

rem check the updates.txt file for required updates. If there are none, just exit
find /i "Required Update:" updates.txt
if errorlevel 1 then exit /b

rem Send an email using blat, or your favorite commandline emailer
blat updates.txt -subject "%computername% needs updates" -to email@domain.com

 

Here is the second script, called updates.vbs. Put this in the same folder as the above script.

 

On Error Resume Next

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

Set searchResult = updateSearcher.Search("IsAssigned=1 and IsInstalled=0 and Type='Software'")
If err.number <> 0 Then
	wscript.echo "Error checking for required updates [" & err.number & "] - " & err.description
	err.clear
End If

req_updates = 0
For i = 0 To searchResult.Updates.Count-1
	req_updates = req_updates + 1
    Set update = searchResult.Updates.Item(i)
    wscript.echo "Required Update: " & searchResult.Updates.Item(i)
Next

Set searchResult = updateSearcher.Search("IsAssigned=0 and IsInstalled=0 and Type='Software'")
If err.number <> 0 Then
	wscript.echo "Error checking for optional updates [" & err.number & "] - " & err.description
	err.clear
End If

opt_updates = 0
For i = 0 To searchResult.Updates.Count-1
	opt_updates = opt_updates + 1
    Set update = searchResult.Updates.Item(i)
    wscript.echo "Optional Update: " & searchResult.Updates.Item(i)
Next

If err.number <> 0 Then
	wscript.echo "Error enumerating updates [" & err.number & "] - " & err.description
	err.clear
End If

If req_updates = 0 And opt_updates <> 0 Then
	wscript.echo "No required updates are ready at this time"
End If

If opt_updates = 0 And req_updates <> 0 Then
	wscript.echo "No optional updates are ready at this time"
End If

If opt_updates = 0 And req_updates = 0 Then 
	wscript.echo "No updates are ready at this time"
End If

wscript.quit

To run it, just execute updates.cmd. You can schedule it as a task to run daily, weekly or just run it whenever you want a quick check on the updates status of a particular machine.



Back to Article Index

blog comments powered by Disqus