Better Living Through Scripting


Yes...Better! Image source

As a sys admin, life is better and less stressful when you know how your Servers are:  Are they living happy and fulfilled lives providing whatever services are required of them or are they moving into chaos?  It makes sense, then, to monitor your environment and find this information out.  There are a lot of products that purport to do this for you, some free, others not.  But did you know….you can also do it yourself… and when you do, you’ll find you live better.

With a bit of home-grown scripting, you can monitor and report various aspects of your servers and network.  You can also begin to get out in front of issues -- finding and correcting potential problems before the end users start phoning.  As you grow your own scripting skill-set, you'll start automating some of the tedious or error prone tasks you are faced with day by day.  Ultimately you'll understand what's happening on your network and with your servers to a depth not previously possible…Through Scripting

With the advent of Microsoft PowerShell, we are entering the “golden age” of Windows Scripting.  There are Powershell command interfaces for Active Directory, Exchange, SharePoint, SQL Server and more.  There is also a thriving third party community and ample resources from Microsoft itself (http://technet.microsoft.com/en-us/scriptcenter/).

Even without PowerShell you can accomplish some powerful tasks.  Armed with only a command prompt or script or some VBScript code you can still live better.

Developing your scripting muscle comes down to the answers to two fundamental questions:

  • What’s in your toolkit?
  • How can you make those tools work together?

Tool Kit

So, what’s in your toolkit?  What IS your toolkit?  The toolkit is a collection of general purpose tools and techniques used to solve problems.  Or, switching to a common metaphor: your toolkit is your collection of Lego™ bricks.  With the right bricks you can easily produce and distribute performance graphs, gather system information and answer adhoc questions (such as “What IP addresses are actually configured on all my servers?”).

Common tools include what I regard as "the UNIX brothers" -- wc, tail, tee, gclip -- as well as free Microsoft and third party tools (SysInternals and JoeWare are particularly good), the standard Windows command list and finally any home-grown tools and scripts you can easily reuse.

How do you get them to work together?  In a word: "Piping". Piping is the common glue provided by the operating system.  Traditionally piping allows you to take the output of one program and send it along as the input to the next program.  For instance, to answer "How many files in this directory" you could go:  dir /b and count them by hand.  If you are a .cmd or .bat script who needs to know this the "by hand" part of the solution won't cut it. So, you'd use DIR with a pipe and the UNIX word-count utility, wc, like this:  dir /b | wc -l.

You don't need to be a script to enjoy a pipe... As another example let's answer the question: What processes are running with a 'non-standard' account?  To answer this you'll need to define 'non-standard' (for instance, anything that is not an "NT Authority" account), and you'll need to be able to enumerate the running processes.  Windows' tasklist command does the heavy lifting (you should run all of these commands from a windows command prompt):

tasklist /fo table /v

Using a bit of piping lets you pare down the output.  Findstr is a standard Windows command (try "findstr /?" from a command prompt for more details).

tasklist /fo table /v | findstr /i /v "nt.authority"

Now a bit of house keeping -- remove the headings so we just get the list.

tasklist /fo table /v | findstr /i /v "nt.authority \=\=\= Image.name"

Want the list in alphabetic order? add: "| sort" (sort is a standard Windows command):

tasklist /fo table /v | findstr /i /v "nt.authority \=\=\= Image.name " | sort

How many processes are there attached to a 'non-standard' account?  add: "| wc -l":

tasklist /fo table /v | findstr /i /v "nt.authority \=\=\= Image.name" | wc -l

Other tweaks are possible in order to tune this to exactly what you require.  But you get the drift: we have used simple command piping to build a "tell me non-standard processes" tool.  With a bit of sprucing up you can turn this pipe into a piece of lego suitable for inclusion in your own toolkit.

In upcoming instantiations of Better Living Through Scripting we'll look at:

  • Building Your Own Lego Set I:  dates
  • Building Your Own Lego Set II: prunes
  • Creating a Polite Admin eMailer
  • Look Ma!  An Excel Screwdriver!
  • When "Script" is a Verb