In my last posting I gave you 5 of what I consider to be 5 of the top 10 things to grasp so that you can be successful in writing powerful VBScripts to make your life easier. In this installment I finish up these trains of thought. So lets dive back in:
5. Always try to use option explicit in your code, especially if the script is longer than 100 lines.
Trust me on this one, using option explicit may force you to think a little bit about declaring a variable before you use it and it may be a pain having a silly error about a undeclared variable when you test your code, but it’s a heck of a lot better than trying to find two places where you’ve spelled a variable two different ways in your code– and creating a hard to find bug because of the different spellings.
4. Learn when to use on error resume next and when to just let the script die.
Sometimes you just can’t cover all the possible errors in your code (though you should try) and turning off error checking is an absolute must. When you do do this, be sure and turn error checking back on after you have passed over the area of your code that could cause a problem (with– on error goto 0)
3. Conditional statements are invaluable, just know what tool is right for the job.
Branching your script because a variable has a specific value, is greater than, or less than something else can be invaluable. Often it is the key to clearer to follow code and getting your script up and running quickly. Knowing which conditional statement (If-then-else, for-next, do-loop, select-case, etc) to use is an invaluable tool to have in your tool belt as a systems administrator.
2. If you do it over and over, then you should script it.
This is my motto! You’ve seen it several times… BE LAZY! There is absolutely no sense in doing something that is repeatable by hand day in and day out. Your time could be better spent spending a few seconds looking at a output from a script to make sure it went well when it ran as a scheduled task. Thus freeing you up to do more (or less) of what you need to accomplish during the day.
1. Everything has been done before, it just may take a little effort on your part to cobble it together.
Almost everything has been done before in a script or other program… it just may not be exactly what you need to get the job done. After searching for a while for a script that does exactly what you want and not finding it— look for several scripts that do pieces of what you need done and put them together into one script that is exactly what you need! Above all else use Google for what it does best, search the web for examples. Additionally, as you find sites that are helpful book mark them for later reference. My blog roll to the right is a good place to start as well, I use these sites myself.