The blog of Matthew A. Smith and Michael Chappell, on topics from A to Z

Windows Scripting and VBScript

by Matthew Smith · November 1st, 2007 · 1 Comment

As you may (or may not) know, I do IT work at a small local school. When I am not busy solving teachers' problems, I am tasked with performing various maintenance tasks on these computers. Almost every system that I deal with runs Windows 2000 (the rest run XP or 2003 Server) and these are running on a wide range of hardware (mostly ancient history by today's standards). Most recently, I've been updating a bevy of software. I first installed updates by hand - a long, boring, tedious, and annoying process. Since necessity (or boredom) is the mother of invention, I quickly decided it would be easier to throw all of the commands into a batch file instead of clicking through each install.

Then I remembered that batch files have been around since the dawn of time and there are better ways to do things (such as Linux). Unfortunately I don't have the power of bash at hand... so I investigated the Microsoft way of doing things - most glorious (or not) VBScript (or JScript). Since I found more VBScripts than JScripts, I decided to tinker around with VBS for a while and see what could be done.

I hate VBS. I keep wanting to use Java or C syntax. Arg. So annoying. But it's better than the command line. With a little hacking (and copying and pasting), I have managed to create a simple "stupid" script that runs the install files. As I develop it more, I'll post up what I've got. For now, we have this:

Visual Basic:
  1. ' create the shell object
  2. Set objShell = CreateObject("WScript.Shell")
  3.  
  4. ' do updates
  5.  
  6. msgbox "Preparing to install Updates.. will take a few moments"
  7.  
  8. ' Flash Plkayer
  9. objShell.Run ".\Adobe\install_flash_player.exe /s",,True
  10. objShell.Run ".\Adobe\flashplayer9_install_ie.exe /s",,True
  11.  
  12.  
  13. ' Shockwave
  14. objShell.Run ".\Shockwave\Shockwave_Installer_Slim.exe /s",,True
  15.  
  16.  
  17. ' Quicktime
  18. if GetOS = "W2K" then
  19.     objShell.Run ".\Quicktime\QuickTimeWin2000.exe",,True
  20. else
  21.     objShell.Run ".\Quicktime\QuickTimeInstaller.exe /passive",,True
  22. end if
  23.  
  24.  
  25. ' Windows Media Player
  26. if GetOS = "W2K" then
  27.     objShell.Run ".\WMP\MP9Setup.exe /Q",,True
  28. else
  29.     objShell.Run ".\WMP\wmp11.exe",,True
  30. end if
  31.  
  32.  
  33. ' JAVA
  34. objShell.Run ".\Java\jre-6u3-windows-i586-p-s.exe /s",,True
  35.  
  36.  
  37. ' Adobe Reader
  38. objShell.Run ".\Adobe\AdbeRdr810_en_US.exe /sAll",,True
  39.  
  40. ' Time Zone Update
  41.  
  42. objShell.Run ".\TZUpdate\DaylightSavingFix.exe /qinstall",,True
  43.  
  44.  
  45. msgbox "Installation Complete"
  46.  
  47. ' Get OS Function
  48.  
  49. Function GetOS()
  50. 'Will work with most versions of WSH.
  51. 'CMD window will not display.
  52.   Const OpenAsASCII      =  0
  53.   Const FailIfNotExist   =  0
  54.   Const ForReading       =  1
  55.  
  56.   Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
  57.   Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
  58.   Dim sTemp, sTempFile, fFile, sResults
  59.   sTemp = WshShell.ExpandEnvironmentStrings("%TEMP%")
  60.   sTempFile = sTemp & "\runresult.tmp"
  61.  
  62.   WshShell.Run "%comspec% /c ver>" & sTempFile, 0, True
  63.  
  64.   Set fFile = FSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)
  65.  
  66.   sResults = fFile.ReadAll
  67.   fFile.Close
  68.   FSO.DeleteFile(sTempFile)
  69.  
  70.   Select Case True
  71.     'Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
  72.     Case InStr(sResults, "Windows 95")> 1 : GetOS = "W95"
  73.     Case InStr(sResults, "Windows 98")> 1 : GetOS = "W98"
  74.     Case InStr(sResults, "Windows Millennium")> 1 : GetOS = "WME"
  75.     Case InStr(sResults, "Windows NT")> 1 : GetOS = "NT4"
  76.     Case InStr(sResults, "Windows 2000")> 1 : GetOS = "W2K"
  77.     Case InStr(sResults, "Windows XP")> 1 : GetOS = "WXP"
  78.     Case Else : GetOS = "Unknown"
  79.   End Select
  80. End Function

The GetOS function came from here. I am still developing this and I have written a function to detect the service pack level:

Visual Basic:
  1. Function GetSP()
  2. 'For NT Systems
  3.   Dim SP_VER
  4.   Set Shell = WScript.CreateObject("WScript.Shell")
  5.  
  6.   Select Case Shell.RegRead ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\CSDVersion")
  7.     Case 256
  8.         GetSP = "SP1"
  9.     Case 512
  10.         GetSP = "SP2"
  11.     Case 768
  12.         GetSP = "SP3"
  13.     Case 1024
  14.         GetSP = "SP4"
  15.     Case 1280
  16.         GetSP = "SP5"
  17.     Case 1536
  18.         GetSP = "SP6"
  19.   End Select
  20.  
  21.   Set Shell = Nothing
  22.  
  23. End Function

That function uses information found here. I will probably refine this script to detect currently installed software (and version, if possible), then only run the updates that are needed. I also need to find a reliable way to run Windows Updates... so stay tuned!

Categories: Technology

1 response so far »

  • 1 Dynamic Loading with eval() // Mar 3, 2008 at 1:50 pm

    [...] now I’m working on a replacement for the nasty VBScript I posted a while back that updates the software installed on the computers at work. Sure, the [...]

Leave a Comment

If you wish, you may log in before commenting.