Posted by: David | October 21, 2010

Windows Updates Via Script / HTA (GUI)

I have now finished the creation of a new tool. As part of my job I regularly have to download and install Windows updates on servers in maintenance windows, over the years I have come to notice a few irritations.

  1. If WSUS is badly maintained some updates are not approved and therefor never installed.
  2. If you are using Remote desktop to access the server the little yellow shield does not always show in your session if it is displaying on the console or another session
  3. If you downloading updates for the server from Microsoft Update and you have a slow internet connection or a lot of updates this process can take a while and you may miss your maintenance window

I recently discovered the Windows Update API and decided to find a resolution to my irritations. So I bring you the Windows Update Downloader HTA

Windows Update HTA Screenshot

This can be used for both the download and installation of Windows updates and if a WSUS server is detected it can easily be bypassed in favour of Microsoft Update.

This was a fairly simple little fellow to write however the asynchronous downloads were a little awkward,

Set dsession = downloader.BeginDownload &_
(gObjDummyDict.Item("DummyFunction"),gObjDummyDict.Item("DummyFunction"),vbNull)

I found that VBScript really doesn’t like the use of callback functions on this Interface call and I had to workaround by creating a dummy class and dictionary object.

Additionally getting a HTA to wait is also not very easy as there is no sleep function you can call to this had to be overcome with a very nasty workaround:

Sub Sleep(intSeconds)
	Dim objShell, strCommand
	Set objShell = CreateObject("Wscript.Shell")
	strCommand = "%COMSPEC% /c ping -n " & 1 + intSeconds & " 127.0.0.1>nul"
	objShell.Run strCommand, 0, True
	Set objShell = Nothing
End Sub

Users please be aware that the HTA will hang when you click on check for updates or (Disable / Enable WSUS) while the script is querying the update server, as the HTA is waiting for the function to return, I may get round to changing this to an asynchronous check at a later point but at the moment the script is functional and serves its purpose well. I only added in the install functionality as it wasn’t much more effort.

Please let me know any problems / feedback you may have with the HTA as I would be interested to know if you find it useful or have issues.

NOTE: Please be aware of the disclaimer as this script is provided “as-is”, without any warranty, whether express or implied, of  its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party produsts or information mentioned in the work are authored, recommended, supported or guaranteed by Sys-Admin.co.uk. Further, Sys-Admin shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

By downloading and using this HTA you are accepting the included disclaimer

DOWNLOAD

Advertisements

Responses

  1. Thank you! I’m developing a program with Windows Update API in C#, and your code (And GUI, i expect to base my C# program on it, because i think that yours is very easy-to-use) was very useful to me. In C#, i just do not applied BeginDownload because i can’t use a dummy class for that (Dictionary is easy to apply). I had found something about CallBack and BeginDOwnload in this post at StackOverflow (http://stackoverflow.com/questions/6172111/asynchronous-events-already-defined-linker-error-windows-update-api) but i guess that a little hard. I think that your method is more easy, but i do not know if i can apply it in C#. If you can help me, i will be grated.
    But, anyway, good work! I tested your program here (Windows 7 SP1 64-bits) and worked very well. (Sorry my bad English, I’m Brazilian)

  2. This is awesome, I have been using this framework now and this works great.
    I did wonder as the strComputer variable is used somewhat can this be run remotely also in some fashion.

  3. Oh I can’t wait to try it at work!!!! Just downloaded and ran at home and it worked GREAT… My problem at work is some of my servers will not download the update. Using SCCM which pulls from WSUS. I can logon to the servers and run this but I’m wondering if you can code the script to allow me to run it on remote servers from my office workstation?

  4. Hi thanks for the comment, you are not the first person to ask if this can be run on remote computers. To be honest is was not something that I considered as I would be running in to the territory of matching the functionality of GPO, Windows update and WSUS, I am not trying to re-invent the wheel. As the current script stands it cannot be run on remote computers, although some of the calls are done through WMI that could easily be pointed at other devices on the network. The main work is done by the Microsoft.Update.Session from the Microsoft Articles this doesnt appear to support remote execution calls. (http://msdn.microsoft.com/en-gb/library/windows/desktop/aa386860%28v=vs.85%29.aspx)

    Thanks

    David


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: