Modular vbScript Framework
Here’s a modular vbscript framework I wrote for deploying software and performing other multi-step tasks across reboots.
I won’t list the whole script in the post because it will get messy with all the wrapping. Instead I’ll just describe the main features and you can download the zip file below. The screenshot will also give you a reasonable idea about how it works. It’s a bit rough and doesn’t have robust error handling – it’s really just to give you some ideas about putting together modular vbscripts.
The main script is a .wsf (Windows script file). I use WSF because you can’t include other script files in a .vbs file. The framework gives us the following benefits:
- Code reuse – We can write core functions that are available across all modules
- Modular development – We can create and maintain modules in isolation
- Extensible – It’s easy to add new functions and modules
- Scripty goodness over unattended reboots/logons
- Log file generation
Here is the file/folder structure of the framework:
\RGFramework.wsf <em><span style="color: #0000ff;"><-Main script logic file</span></em>
\includes\header.vbs <span style="color: #0000ff;"><em><-Header file to declare variables etc</em></span>
\includes\Corefunc.vbs <span style="color: #0000ff;"><em><-</em>Functions which are available across all our modules </span>
\Modules\<em>(ModualName)</em>\<em>(ModualName.vbs) <span style="color: #0000ff;"><-Module files
</span></em><em><span style="color: #0000ff;"> </span></em>
Script logic
Here’s an example of typical framework logic:
- Start a continuous loop
- Get the step which we’re up to from the registry. Registry key doesn’t exist yet (Step 0)
- The Select statement executes the code for step 0
- Set the script to run next time windows starts
- Set auto-logon so Windows logs on automatically when it boots up
- Run a module e.g. InstallOffice()
- Call “NextStepReboot()”. The current step is recorded in the registry and the system reboots
- The system has rebooted and logged on automatically and the script starts again
- Get the step which we’re up to from the registry (step 1)
- The Select statement executes the code for step 1
- Run another module .
- If we don’t need a reboot then we’ll just call NextStep() to record the step we’re up to
- The code loops back around to the select statement and we now go to step 2
- Run another module function
- Clear auto-logon
- clear script start-up
- exit
Note*
To start the script from the beginning again or from a specific step ‘the “HKLM\SOFTWARE\RGFramework\Step” registry string must be deleted or manipulated. This string represents the last step that was run so if it is set to 3 then next the script runs it will execute step 4.
Modules
A module is just a file which contains a single function. The function has the same name as the module file. Here is an example of a module which installs an application. (\modules\installMyApp\installMyApp.vbs)
<span style="color: #0000ff;">Function </span><em>installMyApp</em>
objShell.run strInstallSource&"\MyApp\myapp.exe /quiet", 1, true
<span style="color: #0000ff;">End Function</span>
The module is added to the main script logic file with the following directive.
<script language="VBScript" src="<em>\modules\installMyApp\</em><em>installMyApp.vbs"</em>/>
The module is executed by simply calling the function during one of the steps.
installMyApp()
If you have any questions ask in the comments and let me know if you want more stuff like this 🙂
Here’s the zip file which contains the script structure and code.