Script Tips

Falling to Pieces

Break out of the script-writing tedium and create modular, reusable snippets instead.

One of the things professional programmers go on and on about is modularization, the concept of breaking your code into pieces that can be more easily reused. As administrators, we’re often more concerned with just getting the job done than worrying about niceties like modularization, but smarter programming -- and modularization definitely qualifies -- can help get the job done faster.

First, start writing subroutines and functions in your scripts. Whenever possible, try to break major functionality out into these modules. A fairly common task is writing scripts that output log files. Instead, write subroutines that can encapsulate this functionality:

Sub AppendToLog(sFilename, sMessage)
  Dim oFSO, oTS
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  Set oTS = oFSO.OpenTextFile(sFilename, 8, False)
  oTS.WriteLine sMessage
  oTS.Close
End Sub

This subroutine appends the contents of sMessage to the filename specified in sFilename.

Notice a couple of important things about the module: First, it doesn’t utilize any variables that are declared outside the subroutine. This makes it easy to copy and paste the subroutine between scripts, because the subroutine is completely self-contained. Any information which needs to be passed into the subroutine -- like the log filename and message -- is passed in as arguments. If the module needs to return any information, it’d be written as a function, and the information would be returned as the result of the function.

Once you’ve gotten in the habit of modularizing even simple tasks like writing to a log file, you can start to build a library of useful modules. Many script editors, including PrimalScript http://www.primalscript.com, incorporate "snippet" libraries. You can define your modules as snippets, making it easy to insert them into any script that needs to use that functionality. The idea is to write a script only for a particular task once, and then reuse that script when you need it. You can also build standalone libraries of modules in Windows Script Components (WSCs); I’ve got more information on my site at http://www.ScriptingAnswers.com about WSCs if you’d like to explore that option.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular