Windows Scripting and VBScript

01 Nov 2007

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:

‘ create the shell object
Set objShell = CreateObject(“WScript.Shell”)

‘ do updates

msgbox “Preparing to install Updates.. will take a few moments”

‘ Flash Plkayer
objShell.Run “.\Adobe\install_flash_player.exe /s”,,True
objShell.Run “.\Adobe\flashplayer9_install_ie.exe /s”,,True

‘ Shockwave
objShell.Run “.\Shockwave\Shockwave_Installer_Slim.exe /s”,,True

‘ Quicktime
if GetOS = “W2K” then
objShell.Run “.\Quicktime\QuickTimeWin2000.exe”,,True
objShell.Run “.\Quicktime\QuickTimeInstaller.exe /passive”,,True
end if

‘ Windows Media Player
if GetOS = “W2K” then
objShell.Run “.\WMP\MP9Setup.exe /Q”,,True
objShell.Run “.\WMP\wmp11.exe”,,True
end if

objShell.Run “.\Java\jre-6u3-windows-i586-p-s.exe /s”,,True

‘ Adobe Reader
objShell.Run “.\Adobe\AdbeRdr810_en_US.exe /sAll”,,True

‘ Time Zone Update

objShell.Run “.\TZUpdate\DaylightSavingFix.exe /qinstall”,,True

msgbox “Installation Complete”

‘ Get OS Function

Function GetOS()
‘Will work with most versions of WSH.
‘CMD window will not display.
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1

Dim WshShell : Set WshShell = CreateObject(“WScript.Shell”)
Dim FSO : Set FSO = CreateObject(“Scripting.FileSystemObject”)
Dim sTemp, sTempFile, fFile, sResults
sTemp = WshShell.ExpandEnvironmentStrings(“%TEMP%”)
sTempFile = sTemp & “\runresult.tmp”

WshShell.Run “%comspec% /c ver >” & sTempFile, 0, True

Set fFile = FSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)

sResults = fFile.ReadAll

Select Case True
‘Add more info to the 98 and 95 to get the specific version. i.e. 98SE 95 a,b,or c
Case InStr(sResults, “Windows 95″) > 1 : GetOS = “W95″
Case InStr(sResults, “Windows 98″) > 1 : GetOS = “W98″
Case InStr(sResults, “Windows Millennium”) > 1 : GetOS = “WME”
Case InStr(sResults, “Windows NT”) > 1 : GetOS = “NT4″
Case InStr(sResults, “Windows 2000″) > 1 : GetOS = “W2K”
Case InStr(sResults, “Windows XP”) > 1 : GetOS = “WXP”
Case Else : GetOS = “Unknown”
End Select
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:

Function GetSP()
‘For NT Systems
Set Shell = WScript.CreateObject(“WScript.Shell”)

Select Case Shell.RegRead (“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\CSDVersion”)
Case 256
GetSP = “SP1″
Case 512
GetSP = “SP2″
Case 768
GetSP = “SP3″
Case 1024
GetSP = “SP4″
Case 1280
GetSP = “SP5″
Case 1536
GetSP = “SP6″
End Select

Set Shell = Nothing

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!