Actions Speak Louder
Keep abreast of what's happening using a function that logs and optionally displays script actions.
What the heck did that script do!? No matter; if you are being asked
by a victim of your script or you're asking the question yourself, there
is nothing handier than a script that can record what it's doing. I often
use the same function to post status during script execution. Of course,
sometimes you'll want to log something but not display the script's status.
To handle this I pass not only what should be logged to the function,
but also if it should be displayed so it can be handled case by case by
the same function.
Following best practices, let's use a variable to identify the log file. That way, you can easily change it in the future or be able to vary the name if you're using the script elsewhere with another script:
sLogFile = "c:\temp\mylog.txt"
If later, you decide that you want the ability to force not to display anything regardless of how you called the function, here's a line that sets a global variable to override the setting:
ForceQuiet = False
Because the File System Object is something you will typically make use
of in a script outside this simple function call, let's place the part
that creates the object in the body of the script so that it's available
to any functions in your script (as well as the one you're writing now):
Set oFSO = CreateObject("Scripting.FileSystemObject")
And now for the actual function:
Function LogAction (sMessage, IsVisible)
Set oLogFile = oFSO.OpenTextFile(sLogFile, 8, True)
oLogFile.WriteLine Date() & " " & Time() " | " & sMessage
If IsVisible And Not ForceQuiet Then
The function uses the File System Object to open a text file (and creates
it if the file doesn't exist) and then writes in the date and time followed
by the message that is passed by the script's actions. If IsVisible is
true (also passed to the function) and if the ForceQuiet variable is not
true, the message written to the log is also echoed to the screen. The
function should be called like this:
LogAction "Starting Script", False
LogAction "Mapping Drives", True
Tech Help—Just An
Got a Windows, Exchange or virtualization question
or need troubleshooting help? Or maybe you want a better
explanation than provided in the manuals? Describe
your dilemma in an e-mail to the MCPmag.com editors
at e[email protected];
the best questions get answered in this column and garner
the questioner with a nifty Redmond T-shirt.
When you send your questions, please include your
full first and last name, location, certifications (if
any) with your message. (If you prefer to remain anonymous,
specify this in your message, but submit the requested
information for verification purposes.)
Just call this function whenever you perform an action in your script
and if things should go wrong, you'll see just where the problem is!
Inventory Script Feedback
Bob, I read your inventory script column and I'm really fired up to use
it. I copied what you had in the article; however, when I run it I get
an error at the line that starts with "Set oReg = GetObject ".
It stops at that line and says there is an invalid character on that line
at 1. I guess this would be the "S," but why? I've tried both
a capital and small S, and have tried the script without the Set. Nothing
seems to work. What am I missing?
It may be the & in that line -- I use &_ to tell VBScript to continue
the line of code on the next line. It's helpful for readability and if
copied and pasted it should work, but getting stuff from HTML can result
in odd issues from time to time. To ensure you get it the way I intended,
you can use the one I posted here.
Thank you very much! Is there a way to pipe the results of the output
to a text or .csv file either at the end of the scan of each registry
key or at the end of scanning all of the keys?
A good suggestion. Ive added a second
version of the file that generates a text file. This should be enough
to get you in the right direction.
Bob Kelly is president and co-founder of AdminScriptEditor.com, home to an integrated suite of scripting tools and a shared library of scripts and language help. He has authored books on scripting and desktop administration and several white papers. Bob also owns and operates AppDeploy.com, where he writes and produces videos on topics related to software deployment.