Self Installing VBScripts
18 Jul 2009I like using VBScripts and the Windows Script Host to resolve issues as it has proved quick and flexible for most tasks. One thing I have found problematic on occasion is the installation of the scripts on a user’s PC. It takes time to do if it is not being deployed using logon scripts, group policy, etc. To this end I decided to write a function to allow a script to install itself. The script below is primarily made up from three functions/routines.
Starting from the bottom, the BuildFolderPath()
function just takes a
file path and adds a backslash to the end if it needs one.
The next function is a little more interesting. ScriptPath()
returns
the folder in which the script is being run from. Note that if it is
being run from a shortcut it is the target folder. This script works by
removing the name of the script from the full file path of the script
file.
The last chunk of code is the sub routine InstallScript()
and does the
majority of the work so to speak. This checks to see if the script is
in the correct location. If it is not it asks the user for permission
to copy it from the current location to the one against which it
checked. This does of course rely on the user having appropriate file
permissions to copy the script there in the first place, but if we need
anything more then some sort of executable or direct access by an
administrator is always going to be best otherwise you’ll be handing out
administrator passwords verbally or in plain text in script files. Once
installed (or if the user chose not to install) there is a bit of
feedback to the user and the script ends. If the script is running from
the right location, no further action is taken and the script
continues.
Option Explicit
Const INSTALL_TO = "%UserProfile%\SendTo"
InstallScript INSTALL_TO
Msgbox "The script is being run from the right place..."
'This routine checks if the script is in the right folder location and if it
'isn't it will give the user the option of copying the script to the right location
Sub InstallScript(p_strInstallTo)
Dim strInstallToFolder
Dim objShell, objFSO
'Set the installation folder
Set objShell = CreateObject( "WScript.Shell" )
strInstallToFolder = BuildFolderPath(objShell.ExpandEnvironmentStrings(p_strInstallTo))
If Not strInstallToFolder = ScriptPath Then
'Copy the script to the correct folder if the user agrees
If Msgbox ("This script is not being run from the expected location." & vbCrLf & _
"Would you like to install it to " & strInstallToFolder & " now?", _
vbYesNo + vbQuestion, "Install Script") = vbYes Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile WScript.ScriptFullName, strInstallToFolder & WScript.ScriptName
MsgBox "The script has been installed/updated.", vbOkOnly & vbInformation, "Install Complete"
Else
MsgBox "The script has not been installed and will not run.", vbOkOnly + vbExclamation, "Install Cancelled"
End If
'Finish the script at this point
WScript.Quit
End If
End Sub
'This function returns the path which the script is running from
Function ScriptPath()
ScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
End Function
'This function just adds a trailing backslash if there isn't one
Function BuildFolderPath(p_strPath)
If Right(p_strPath,1) = "\" Then
BuildFolderPath = p_strPath
Else
BuildFolderPath = p_strPath & "\"
End If
End Function
An interesting thing to note is that I’ve passed folder paths through the ExpandEnvironmentStrings() function. This converts any environment variables in the string and allows them to be converted to their full path. If you want to know more about environment variables I’d recommend a few minutes browsing this page on environment variables in Windows XP.
So all you have to do is decide on a folder where you want the script run from and then pop these functions into your script. Personally I like to create shortcuts for scripts too (e.g. in the user’s send to folder, start menu or the desktop). I’ll be posting a little later on how to add that to your toolbox - particularly useful for installations like this.