How many of us have searched incessantly for “that script” we wrote/borrowed/stole 2 years back? OK, I still do that sometimes but I have a method that drastically reduces this usually fruitless but always frustrating script-hunt. This method leverages Windows Script Host’s capability to reference another script at execution time, simlar to the way programming languages allow you to reference an “include” file. Basically there are 2 steps to start taking advantage of this sanity-saving technique.
Step 1 – The “Include” File
Create your “include file”. This is simply a VBS file that has all of the reusable Functions and Subs you have found helpful over the years. I call mine “Global.vbs”. You can download the one I use here (rename it with a VBS extension) as a starting point, but be sure to add your additional functions/subs as needed. Whenever you find or write a cool function/sub, make sure it is well tested then add it to your include file. If you downloaded mine note the comments at the top which describe the functions/subs contained. I recommend keeping this comment section up-to-date so that it is easy to see what goodness the file contains. Once the include file is complete place it in a location that is accessible from anywhere on your network. If you use Active Directory I suggest placing this in the sysvol SCRIPTS directory (the old NETLOGON share). That way it will be replicated and accessible from all your domain controllers, can be updated from one place, and when called, the most “local” version will be used (thanks to AD Sites).
Step 2 – The “Main” Script
In step 1 you created an awesome repository of tried and true code. Now to put it to use. Create a text file called EXAMPLE.WSF and paste the following in it:
<job> <script language="VBScript"> option explicit Dim TotalRunTime TotalRunTime = Timer() </script> <script language="VBScript" Src="\\SERVER\SHARE\global.vbs"/> <script language="VBScript"> '*************************************************************************************** '*************************************************************************************** 'This is a blank script file that includes global.vbs. ' ' Version 0.1 ' Author ' Arguements: '*************************************************************************************** '************************************Insert user code below***************************** '************************************Insert user code above***************************** wscript.echo "Total Run Time = " & timer() - TotalRunTime & " Seconds" </script> </job>
Keep this EXAMPLE.WSF handy so you can use it as a template to create your scripts. Everytime you need to write a script just make a copy of EXAMPLE.WSF and write your code between the “Insert code Here” lines. As you write it and need to reference a function/sub that is listed in the “include” file, just reference it as though the function/sub was already in the script you are writing. Remember that all of the reusable code from your “include” file is read in during execution time so it is all available to call.
As implied in the title of the blog entry, the main benefit of this approach to scripting is that you reuse your code…no more re-writing the same script you wrote (and lost) several times in the past. And when you don’t have to re-write code you don’t have to re-debug all the same mistakes you made in the last several times you wrote the script. Additional benefits include smaller scripts (you don’t have the functions/subs in your main script), and easier scripting for the VBS-uninitiated (all the hard stuff is already written…just call it).
Disclaimer - I did not invent this “Mac Daddy” process but I have used the heck out of it. All thanks go to Tommy Mills, one of my many scripting mentors…Thanks Tommy!
