The VBScript Network and Systems Administrator's Cafe:

February, 2008

Feb 29 2008   10:30PM GMT

Using error control in a vbscript to recover from odd errors



Posted by: Jerry Lees
Development, VBScript, Error control, Windows Management Interface

In my last installment, Finding HP Printers using PCL6 Drivers with WMI, I gave you a handy script snippet that showed you how to pull the names of the printer drivers for your printers shared on a print server. In the script I used a few pieces of code that may not be familiar to everyone. In the next few installments we’ll explain better what the script does and how it does it—so hopefully you can begin applying the knowledge elsewhere…

The script was basically (line numbers included as a reference):

1. On Error Resume Next
2. 
strComputer = “.”
3. Set objWMIService = GetObject(”winmgmts:\\” & strComputer & “\root\cimv2″)
4.
Set colItems = objWMIService.ExecQuery(”Select * from Win32_PrinterDriver”,,48)
5.
For Each objItem in colItems
6. 
    Wscript.Echo “ConfigFile: ” & objItem.ConfigFile
7. 
    Wscript.Echo “DataFile: ” & objItem.DataFile
8. 
    Wscript.Echo “DriverPath: ” & objItem.DriverPath
9.
     Wscript.Echo “Name: ” & objItem.Name
10.
   Wscript.Echo “OEMUrl: ” & objItem.OEMUrl
11.
   WScript.Echo “_________________” & vbcrlf
12.
Next

In line 1, the statement On Error Resume Next may be new to you. This statement basically keeps your script from breaking completely and not completing due to a minor error. The down side of this is that if your code isn’t tightly checking for errors it will continue to run with potentially bad logic and cause quite a bit of havoc on your network. IMHO, you should try to avoid it where ever possible. In this case it wasn’t possible because there were to many unknowns.

As an FYI, The brother to On Error Resume Next is On Error Goto 0, basically this turns error checking back on. So, if you need to use it in an area where you’re not certain a piece of code may or may not encounter an error from time to time it’s best to turn error checking back on after that piece of code completes its work.

Consider this example code that has errors:

Wscript.echo “Stepping through ‘Y’ from 1 to “ & X
On Error Resume Next
Wscript.echoo “Error in this line!!!!”
Wscript.echo “Did this line print?”

Of course, if you run this it won’t complete, it will display something similar to:

Stepping through ‘Y’ from 1 to
C:\www.websystemsadministration.com\errorcontrol.vbs(2, 1) Microsoft VBScript runtime error: Object doesn’t support this property or method: ‘Wscript.echoo’
 

This tells us there is an error in line 2 of the script… and gives us a slight hint (sometimes) of what the error is. In this case, we’ve user a function of the Wscript object that does not exist, echoo. Of course we want to know about that. But suppose we didn’t? Well, this would stop vbscript from complaining:

Wscript.echo “Stepping through ‘Y’ from 1 to “ & X
On Error Resume Next
Wscript.echoo “Error in this line!!!!”
Wscript.echo “Did this line print?”

Now it displays:

Stepping through ‘Y’ from 1 to
Did this line print?
 

Notice it skipped the line with an error and went to the next line? To turn error checking back on we just adjust the script this way—but we add a final line with an error to see if it breaks on an error:

Wscript.echo “Stepping through ‘Y’ from 1 to ” & X
On Error Resume Next
Wscript.echoo “Error in this line!!!!”
On Error Goto 0
Wscript.echo “Did this line print?”
Wscript.echoo “What about this new line?!?!”
 

Still the same output, but now you see an error… because we turned error checking back on!

Next installment, I’ll  Explain the Set and the Getobject section of the code.

Extra credit: There is a “soft error” in the script that would never cause the script to break, but cause you TONS of debugging time. Did you find it in the code … or the output? Post a comment and I’ll let you know what I think it is in the comments section in a few days!

Feb 29 2008   2:45AM GMT

Finding HP Printers using PCL6 Drivers with WMI



Posted by: Jerry Lees
Development, Networking, VBScript, Printer Administration, Printers, Error control, Windows Management Interface

Recently, at my work we had a situation where users were reporting the printer was not printing their documents and/or it was printing very slowly. After digging into the problem, I found:

1. the documents were spooling just fine to the printer

2. the documents were not hanging in the printer

3. Sometimes there were documents printing correctly when the user printed

4. on occasion the documents “didn’t print”

Of course, the nearest printer doing it was on the floor below me… I hate stairs. ;-) After digging into it, wasting a ton of paper– and burning untold amounts of Mountain Dew and Dorito calories; I found that the print jobs were always producing printouts! Sometimes it was the document I printed and sometimes it was a single page with the following message in the documents place:

error:

Subsystem:  KERNELError:  IllegalStreamHeaderOperator:  0×0

Position: 0

 The operator and position line held various values, but was always the same first 3 lines. What I found after extensive digging was that this issue was a PCL6 error message. Some people were saying it was a network error and others were saying it was a server print driver error– Don’t they always say it’s one of these, regardless of the problem?

Those who were saying it was a network error were advising that I pulled out a sniffer, like wireshark because packets were being corrupted. I didn’t think this was good advice. First, pulling a sniffer out to troubleshoot printing was like pulling a blowtorch out to light a campfire… sure it will work, but it’s a lot of extra work! Also, I knew that Microsoft printing is done like most things Microsoft– over TCP ports 138,139, and 445 (I believe)– most importantly TCP, since TCP has error correction built in via a checksum.

The others who were saying it was a server issue were recommending that I upgrade ALL the printer drivers on the server. This was quite an undertaking since there were 40 or so printers on the server of various models– most of them made by HP. Knowing HP, the drivers were bundled together into one package and upgrading the drivers for one printer would likely change a bunch of printers. To much risk! 

I did find a few needles in the google hay stack, that were saying the problem was typical for PCL6 and to simply switch the driver back to PCL5. Since both driver types were installed when I installed the printer drivers (always a good idea) I thought I’d give it a shot. Tah Dah! It worked like a charm, with no changes in the documents printer that I could see.

Now that I had one printer fixed, but had to point and clickety click to find the other’s using PCL6 drivers. To much work! So I wrote a quick script to get the bulk of the printer drivers that were being used by the print server so I could focus on the ones needing changing.

Here is the script source, copy it to a .vbs and either; run it from the print server as is or change the second line between the quotes from a period to the name of the print server: (Note: I recommend you run this from the command line with cscript scriptname so you don’t get bombarded by popups)

On Error Resume Next

strComputer = “.”
Set objWMIService = GetObject(”winmgmts:\\” & strComputer & “\root\cimv2″)
Set colItems = objWMIService.ExecQuery(”Select * from Win32_PrinterDriver”,,48)
For Each objItem in colItems
    Wscript.Echo “ConfigFile: ” & objItem.ConfigFile
    Wscript.Echo “DataFile: ” & objItem.DataFile
    Wscript.Echo “DriverPath: ” & objItem.DriverPath
    Wscript.Echo “Name: ” & objItem.Name
    Wscript.Echo “OEMUrl: ” & objItem.OEMUrl
    WScript.Echo “_________________” & vbcrlf
Next

That’s the end of the script! You’ll want to look at the “Name:” lines for the string PCL6. Not all drivers have PCL6 in the name, but a good majority do so it should save you some time!

Take a bit of that time to leave me a comment letting me know if this was helpful!

 Good luck!


Feb 20 2008   5:12PM GMT

Expert Says Scripting is Key to Career Advancement.



Posted by: Jerry Lees
DataCenter, Development, IT careers

SearchWinIt has an interesting podcast that caught my eye just now, titled Microsoft issues largest patch bundle since August. However, the truly interesting thing to me was said around 5minutes 59 seconds into it. They said Brian Desmond, an Active Directory MVP, said that “Learning any scripting or programming language is especially important for those hoping to land jobs with larger companies”. Brian’s got some good stuff on his blog, so be sure an check it out here.

Immediately after they mentioned another (unmentioned) expert said admins could get by without learning any scripting in companies with  ”30 users or less.”


Feb 20 2008   7:47AM GMT

Using Windows Management Interface (WMI) to Make Your Life Easy.



Posted by: Jerry Lees
Development, DataCenter

In this blog entry I want to setup the next few blog entries that will deal with a technology that is quite possibly one of the most powerful tools in a Network Administrators tool chest… the Windows Management Interface or WMI.

WMI is a powerful tool when combined with scripting, you can use it to gather a vast array of information very quickly from a system or a number of systems. It is pretty easy to use once you have a basic script to setup the objects to make the calls, you know the Win32 class you want to call to query the information, and when you know what properties and/or methods to use. (no, this isn’t exactly the same as the C win32 classes or the windows foundation classes)

Think of Methods as subroutines or functions to accomplish a specific task, like starting a service. Think of properties as variables that you can check the value of to do something.

WMI can be used to also make configuration changes as well– imagine the company you work for just bought another company and you need to add their DNS suffix to all your servers and client machine’s TCP/IP properties’ search order and there are several hundred machines. Would you touch them all over a weekend? No! Be Lazy, and gain the respect of your new bosses if you were on the other side of the buy out! ;-) Do it with WMI in under an hour while you sip a Mountain Dew.

Another possibility is, say you have a Production SQL server that needs a RAM upgrade but no one can recall exactly what the memory configuration is that makes up the 4Gb of RAM in it. Was it 4 1Gb sticks, or 8 512Mb sticks? Were there any free slots? Guess you’ll have to come in during the 3am maintenance window (for 5 minutes) to figure it out so you can order the right parts. No! Be Lazy, WMI can tell you EXACTLY what the memory configuration is to make up the total and how many slots are available/free!

 Here is a good introduction article on WMI for you to look over and read. After reading that article you might want to poke around the Microsoft Documentation for the Win32 Classes for WMI. Be sure and bookmark the win32 classes page– it will be invaluable to you.

To make a call from a script to WMI you have to “create the WMI object” you want to work with. This is done in just a few lines of code. Then you actually create the object with a SQL like call like any of the following (These are examples to get the juices flowing… I haven’t tested them):

  • Select * from Win32_PrinterDriver
  • Select Caption from Win32_BIOS where Releasedate < ‘07/01/07′
  • Select * from Win32_Service where Started = True

After that you treat the object you created just like any other object, you just have to know what the properties and methods are, here is a highlight of some of the properties and methods for Win32_Service.

The properties are:

AcceptPause
AcceptStop
Caption
Description
DisplayName
ProcessId
Started
State
Status

Example Methods are:

StartService
StopService
Create 

There are others, but I think you’ll get the general idea. Next time, I’ll be sharing a quick script to display Device Drivers that are in use by printers on a server or workstation. Until then… Be Lazy!


Feb 16 2008   9:24AM GMT

Getting Started Writing VBScript to Administer a Windows 2000 or 2003 Network



Posted by: Jerry Lees
VBScript, DataCenter, Development

For my first Blog entry I’d like to create a list of software you’ll need (along with some recommendations) to easily follow along with my next posts. Some of you will already be ahead of the game here, but if you’ll allow me just a bit of time to get others up to speed and get started I assure you we’ll get to the heart of some good scripts and tips soon enough. So check back often or add the blog to your favorite RSS reader.

 VBScript can sometimes be a daunting technology, in fact, some network administrators shy away from it because they believe it offers them no benefits. I disagree.  VBScript offers a network administrator a vast array of benefits– most revolving around making your life easier and reducing the number of late night hours worked by automating redundant tasks.

 Think of virtually any task that you hate doing, for example:

  • Something you have to get up in the middle of the night to accomplish
  • Something that requires a bunch of mindless clicking tasks
  •  That odd server that occasionally needs a reboot in the middle of the night
  • Gathering (or changing) configurations on all the servers or workstations you manage
  • Reports for the boss
  • Finding Services running as a specific account

I could go on and on, but you get the picture I think. Sound good!?!?!?

 Here are a few things you will likely need to get started:

  1. A VBscript editor
  2. Nothing else!  ;-)

Now, you can edit vbscripts with notepad or wordpad, textpad, or any other text editor you chose however I would recommend one that has color coding so that you can more easily read the code you are writing. Some cost and some are free. Remember, you generally get what you pay for, but there are some very good free/cheap ones out there.

My personal favorite is PrimalScript from Sapien Technologies, they have a 30 day trial available at the link I’ve provided. The software’ss registration cost is roughly between $179US and $379US . It’s IMHO the Cadillac of script editors and I’ve used it several places I’ve worked. Unfortunately, I do not have a personal license of my own so until I can get one– I’ll be using a FREE piece of software that will likely work for almost every one’s needs… VBSEdit. Now, this piece of software doesn’t require registration to use it but it does have a few nag screens here and there until you register. It really is a great piece of software and if you do find you like it I highly encourage you to register it since it’s only about $50US.

 So… Go ahead and download one of these tools and we can get started!