The VBScript Network and Systems Administrator's Cafe:

DataCenter

Apr 16 2008   7:11PM GMT

VBScript to check CPU and Processor performance counter statistics using WMI



Posted by: Jerry Lees
Functions, DataCenter, Development, VBScript

By now, if your reading my blog you’ve had a chance to play with the script I posted for Using VBScript to gather server performance counter data with WMI . But I think I might know what you’re thinking– “Hey, that’s cool… but I could careless about knowing how much non-paged pool memory there is being used on the system! I need to track something useful, like processor utilization or  something else!

Well, you can– and here you go!  As always the code itself is available on my website in the File Depot if you’d like to avoid copy/paste problems or you don’t want to type the code yourself.

This script has two functions in it, GetFormattedCPU() and GetRAWCPU() which both do the same thing, except that one uses a raw performance counter (Win32_PerfRawData_PerfOS_Processor) and the other uses a preformatted counter (Win32_PerfFormattedData_PerfOS_Processor) to get the information from the system. These two counter types, as you will see, give you very different numbers and for the most part you should probably stick with the preformatted counters if at all possible so you don’t have to do a ton of math to get meaningful data.

The functions both return a Comma Delimited string that contains the CPU percentage usage for all processors in the system. Note that, on my dual core system I get 3 numbers– respective to the return value; Processor 0, Processor 1, and _Total. This should work equally well on Physical SMP systems that are either single or Dual Core, but I suspect on a single core system with more than two processors you will get two numbers that are identical… unfortunately, I no longer have a single core system I can test on.

As always, the code is written in such a way that you can replace the “.” with a “server name” and it will work remotely on a system– providing you have administrative rights. Here is the code:

Option Explicit
On Error Goto 0
Dim strSrv, strQuery

strSrv = “.”

WScript.Echo GetFormattedCPU(StrSrv)
WScript.Echo”______”
WScript.Echo GetRAWCPU(StrSrv)

Function GetFormattedCPU(strSrv)
    Dim objWMIService, Item, Proc
   
    strQuery = “select * from Win32_PerfFormattedData_PerfOS_Processor”
    Set objWMIService = GetObject(”winmgmts:\\” & StrSrv & “\root\cimv2″)
   
Set Item = objWMIService.ExecQuery(strQuery,,48)
    WScript.Echo strQuery
    For Each Proc In Item
       GetFormattedCPU = GetFormattedCPU & Proc.PercentProcessorTime & “, “
       wscript.echo “Processor ” & Proc.Name & ” = ” & Proc.PercentProcessorTime
    Next
 
End Function

Function GetRAWCPU(StrSrv)
      Dim objWMIService, Item, Proc
    
      strQuery = “select * from Win32_PerfRawData_PerfOS_Processor”
   
      Set objWMIService = GetObject(”winmgmts:\\” & StrSrv & “\root\cimv2″)
      Set Item = objWMIService.ExecQuery(strQuery,,48)
     WScript.Echo strQuery
     For Each Proc In Item
         GetRAWCPU= GetRAWCPU & Proc.PercentProcessorTime & “,”
         wscript.echo “Processor ” & Proc.Name & ” = ” & Proc.PercentProcessorTime
    Next
 
End Function

That’s it!! As a point of reference, the output of this script looks as follows: 

select * from Win32_PerfFormattedData_PerfOS_Processor
Processor 0 = 0
Processor 1 = 0
Processor _Total = 0
0, 0, 0,
______
select * from Win32_PerfRawData_PerfOS_Processor
Processor 0 = 273670781250
Processor 1 = 275960625000
Processor _Total = 274815703125
273670781250,275960625000,274815703125,

Again, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems I’ve provided the code for download for you. From here on out I will make the code available for download from my website’s (www.webstemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

Extra credit: Can you guess why I would have messed up your output from the functions in this script by separating the numbers with commas? I invite you to leave a comment and I’ll let you know if your on the same track as I am. Comments are always welcome!

Mar 7 2008   10:22PM GMT

Eventlog search tool — Find quick help with windows event log entries



Posted by: Jerry Lees
DataCenter, Exchange, Networking, Security, Administration tools

While not VBScript related, I found this Microsoft Eventlog and Error Message Search a few days ago as I stumbled around looking for tools that I thought network administrators would need on my site that I’ve been toying with– I had to share the tool with you as well!

 The tool itself is an awesome resource, similar to EventID.net– except it’s FREE and it comes from Microsoft themselves providing links to Microosft content about the event entry or error message.

 You can search for any combination of the following:

Microsoft Product|
Version
Message ID
Event Source
File Name
Language

When you do it takes you to a search result page that lists the available results for your specific search, each seem to have not only an example of the message, but also an explanation of what the event means…. and the best part a section entitled User Action that gives a possible solution for the problem!

 Enjoy the tip!


Mar 4 2008   1:57AM GMT

Determining properties and methods used in a WMI object for use in a vbscript.



Posted by: Jerry Lees
VBScript, DataCenter, Development, WMI, Windows Management Interface

In this entry we’ll take a look at the microsoft documentation for win32_printerdriver and determine how we can determine what values to use to get data once we’ve created a script to get set of data back from a WMI object into a collection object. Most of the previous discussion can be read, in order in my blog but here are links in order, if your just tuning in:

Finding HP Printers using PCL6 Drivers with WMI
Using error control in a vbscript to recover from odd errors
Using Set, GetObject, ExecQuery, and For Next in VBSCRIPT to use WMI objects

Microsoft has a great deal of documentation here on the win32 WMI classes.  However, here is an excerpt from their win32_Printer class. Most of their documentation is written for c/c++ programmers it seems– don’t let that scare you off, you don’t need this code in yours nor do you need c/c++ experience to use them.

Below you see the first line creates a “class” called Win32_PrinterDriver that is of a type called CIM_Service. Essentially, a class contains data and tools you can use in your programs. CIM_Service is just the name of the data type the class is created as– you’ll see references to CIM all over the place with WMI. It stands for Common Information Model, check out the preceding link for more information.

After that, the items in curly braces ( {} ) are the actual names of the properties and what types of data they contain. Again, as long as you are using the data in strictly Vbscript code you don’t even need to know what the type is– VBSCRIPT takes care of that for you. However, on ocassion you’ll see a “type mismacth” error in your code. This is simply referring to the fact that you have put a value in a variable of one type (a string for example) into a variable that is meant to have another type (an integer for example), typically this happens when you use a external component that was written in another language that is more strict about the data types than vbscript, like c, c++, or others. (Which is almost all of them outside of the Visual BASIC family)

class Win32_PrinterDriver : CIM_Service
{
  string Caption;
  string ConfigFile;
  string CreationClassName;
  string DataFile;
  string DefaultDataType;
  string DependentFiles[];
  string Description;
  string DriverPath;

};

In the code I wrote for this series I used Wscript.echo to print the data to the screen inside the For Each/Next loop, like so:

Wscript.Echo “DriverPath: ” & objItem.DriverPath

Remember, objitem was a collection of printer driver information objects?
Notice the last part of that line, .DriverPath?
Notice the last line I gave you from the microsoft documentation?

Yeap, they are both DriverPath! There’s a reason… they are the same thing! Essentially, ANY of the items listed in the Win32 classes can de used in conjuction with a collection object– providing you have collected the same type of object with a WMI object before hand! So this documentation is like GOLD for a systems administrator! So, if you didn’t take a hour or 50 before when I gave you the link check it out here again.

For example, I didn’t use the property Version in my original code. If you needed this information in the program you could add another line in the For Each/Next loop to gather that information as well! Like so:

WScript.Echo “Version: ” & objItem.Version

You can do that with any of the properties and methods in the win32_printerdriver class, and it expands beyond that to all of WMI.

One thing to keep in mind with much of WMI is that it is dependent on the driver or hardware manufacturer to provide the information– not all manufacturers provide that information 100% reliably, so you’ll have to play and tweak what your hardware vendor does give you. My bet is even if it’s not 100% of all the information available, it will be more than you had to start with without hours of digging.

So, here is the original code again. Take a few minutes to play around with the code and the win32_printer driver documentation and see what interesting pieces of information you can pull out of WMI.


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 concludes this series!

 Extra Credit: Can you modify the code above to pull actual printer hardware/software data from Win32_Printer? Hint: stay clear of the properties with a [] after them for now, they will potentially trip you up. And also, remember just because it pulled back one value that was blank doesn’t mean you did something wrong– some items aren’t populated by hardware vendors to WMI unfortunately.


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!