The VBScript Network and Systems Administrator's Cafe:

Remote management

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, Remote management, PerfMon, Windows Management Interface, Administration tools

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 pre-formatted 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 teh pre-formatted 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 percantage 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 identicle… 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!

Apr 2 2008   2:49PM GMT

Using Vbscript to gather server performance counter data with WMI



Posted by: Jerry Lees
VBScript, Development, Windows Management Interface, Functions, PerfMon, Performance Monitoring, WMI, Remote management

In my last entry I shared with you how to use WMI in Vbscript to reboot a server from a scheduled job (either locally or remotely) and I hinted at a scenario where I needed to use this script because a server had a memory leak in an application (Trend Micro OfficeScan 8.0, to be exact). They’ve got a fix out but we just upgraded to 8.0 and apparently that didn’t go well, plus this patch is a server patch that immediately rolls out to the clients– so they are less than willing to jump onto the razor’s edge with a patch. (Politics– We’ve all been there, right?)

 Well, in this entry I’m going to share the function I wrote to pull Poolmemory Non-Paged Bytes from the server using Win32_PerfFormattedData_PerfOS_Memory. I’ll not go into detail about this class and its functions and members here in this posting, but you can find out more details at Microsoft’s MSDN site here. Remember from a previous post (Determining properties and methods used in a WMI object for use in a vbscript), that you can always find out what properties are available by looking at the class definition of these pages.

 The function I wrote pulls the PoolnonpagedBytes property from the Win32_PerfFormattedData_PerfOS_Memory object. This function requires you to pass it the last value of the performance counter. While it doesn’t use it to get the data it does internal to the function compute the delta of the last and the new value– which is handy if your logging and/or monitoring in real time troubleshooting.

The following is a function only, not a complete vbscript to save space and to focus in on this part of the code. For a complete working example script that is tehone I’ve been talking about in the last two posts go here that combines the previous posting’s reboot function, this function, and takes into account the number of times a value can exceed a threshhold during a time frame look here in the vbscript section for “non-paged pool memory“.

Function GetMem(last)
     Set oLoc=CreateObject(”WbemScripting.SWbemLocator”)
     oLoc.Security_.ImpersonationLevel=3
     oLoc.Security_.AuthenticationLevel=WbemAuthenticationLevelImpersonation
     Set owmi=oLoc.ConnectServer(strSrv,”root\cimv2″)
     Set oRef=oWmi.ExecQuery(strQuery)

     For Each detail In oRef
         Newval = detail.PoolNonpagedBytes
         WScript.Echo Newval & vbtab & Newval-last
         getmem = (Newval/1024)/1024
     Next
End Function

Obviously, you have to do some work before you call the function setting up both a variable to recieve the return and also some sort of variable to keep track of the last value. If you aren’t after tracking the changes and only want the return value you can pass any number, including zero, to the fuction and remove the wscript.echo lines in the above code snippet.

Also, since it’s a snippet and as I write this I noticed that you will need some setup in the main routine like:

  •  a variable in your main script called strSrv that has the name of the server you want to pull the value of the counter.
  •  The following line in the main routine that sets up the “SQL” call for WMI and initializes the value of strQuery. (Btw, for a good back to basics look at SQL calls, check out the SQL Server with Mr. Denny Blog here on ITKE.)

              strQuery=”SELECT * From Win32_PerfFormattedData_PerfOS_Memory”

You can find more performance monitor classes that are available to you here. Basically, if it’s available in perfmon– you can get it with vbscript! Just look for classes named like Win32_PerfFormattedData…. or Win32_PerfRawData…, these classes will get you what you want. Keep in mind that Raw data is just that and most times you are going to want to get PrefFormatted data in the beginning. If you’d like more information on the differences, post a comment and I’ll come back to it later.

Again, this code snippet works perfectly in my original script, but sometimes when you strip a piece of code out of a script it doesn’t work right when you plant it somewhere else. Also, 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 complete original script code (and this snippet) for download for you from my website’s (www.webstemsadministration.com) File Depot under the Vbscripts and ITKE Blog Scripts category respectively.

Enjoy and happy scripting!

Extra credit: This is double extra credit, I guess, in my last entry I gave you a hint at my last extra credit question in my previous post– I just made the call a different way. The last posting asked you about what this part of the code did:

{impersonationLevel=impersonate,authenticationLevel=Pkt,(Shutdown)}

Does this help give you more information to go on?


Mar 27 2008   3:03PM GMT

Rebooting or Shuttingdown a Server Remotely with VBScript



Posted by: Jerry Lees
Windows Administration, Remote management, Functions, Development, VBScript, Subroutines

In this installment I’d like to share with you a scriplet I developed out of frustration with not having found this information anywhere else.

 On occasion you need to reboot a server, sure you can do it by logging in or by the shutdown command on windows XP. Heck you can even do it with the Windows 32 API calls in C++! However, sometimes you don’t want to be up and awake when you need to reboot a server. Plus, you might want to have some sort of logic behind it like instead of rebooting every night blindly you might want to check to see is a process is running (like a windows service) and stop it in a controlled manner before rebooting the system– we’ve all go those one off servers that have some weird application that needs fiddling with before you reboot, or atleast the lore at the office says you need to do something first. Right?

Well, I recently had such a situation and became frustrated that I couldn’t reboot a server cleanly with in a script without first executing from within the script, as a child process, a batch file to do the reboot for me. Sure it works… and the main reason I didn’t like it was it wasn’t as sophisticated as I’d like. But another problem is that I would have no real way of knowing what the outcome of the reboot process– in other words, I wouldn’t be able to handle the fact that the reboot didn’t occur or the batch file had an error because windows would have returned only that the command prompt executed and the batch was ran.

In two previous posts, I’ve provided information on Functions, WMI, and Methods to lead up to this topic. If you missed them you might go back and read the quickly so you’ll be up to speed. The posts are:

Working with Subroutines and Functions in VBScript
Using Windows Management Interface (WMI) to Make Your Life Easy.

 In the following script, I create a function called Reboot that uses WMI to create a connection to a server name you pass to it (Note: to protect you I have set the call to reboot() in the script to use “.”, which is the local machine. Change “.” to “ServerName” to reboot a specific remote machine. In other words… run this code unchanged and your desktop will reboot in the next 30 seconds.)

Reboot(”.”)

Function Reboot(StrSrv)
   Set objWMIService = GetObject(_
   ”winmgmts:{impersonationLevel=impersonate,authenticationLevel=Pkt,(Shutdown)}!\\”_
   
& StrSrv & “\root\cimv2″)
   Set colItems = objWMIService.ExecQuery(”Select * from Win32_OperatingSystem”,,48)
   For Each objItem In colItems
        WScript.Echo objitem.Reboot
   Next
End Function

This is a really useful scriptl function, when combined with other pieces of code to gather performance monitor counters (later I’ll post some code examples on this) you could reboot a server if the non-paged pool memory reaches a point where the system is probably going to lock up due to a leaky app or driver… this was my issue I solved with vbscript. 

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 figure out what the following part of the code does?{impersonationLevel=impersonate,authenticationLevel=Pkt,(Shutdown)}