Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Monitor % Process CPU Usage (specific process)Helpful Member! 

digimahn (IS/IT--Management) (OP)
1 Nov 02 18:57
I have asked before...

I think the subject is a little more "descriptive" this time and I hope I can get the help :) It's been quite a few months of on/off research and testing and now another month pretty much dedicated to frustration has passed by.

Object of script:
  1. Pause Default Printer

  2. Start and run an "invoicing" application

  3. When app goes Idle - terminate app

  4. Scan \WINNT\system32\spool\PRINTERS\*.SPL files for specific "data" and delete those .SPL files - allocate a sleep value of .65 - .9 seconds per .SPL for search to complete (with progress display)

  5. Stop and Start Print Spooler service

  6. Refresh default printer queue display

  7. Un-pause default printer (if service is not restarted and queue not refreshed then event log will contain 1 error message for each item missing .SPL - assumes default print server event log options enabled)

Now where I get stuck and have been stuck it to be able to tell when this crappy VB application has gone idle (3) and the only way I see doing that is via WMI.

Using WMI I can get at the "Win32_PerfRawData_PerfOS_Processor" raw data performance counter class - specifically "PercentProcessorTime" ... but my question is: is this where the column in taskmanager labled "CPU" is derived from? This is the only value that seems to make sense after reading the WMI SDK for the past week.

I can't seem to figure out how get this info per process...
I used line items so you can easily see each line of code/script

  1. strComputer = "."

  2. Set objWMIService = GetObject("winmgmts:" & _
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  3. Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_process")

  4. For Each objProcess in colProcesses

  5.     sngProcessTime = ( CSng(objProcess.KernelModeTime) + CSng(objProcess.UserModeTime)) / 10000000

  6.     Wscript.echo objProcess.name & ":  " & Round(sngProcessTime,2) & " Seconds"

  7. Next

I figured out how to get at the time column and convert it to seconds - WHOOPIE! I don't need that...

Then I found this while reading about WMI on MSDN:
  1. Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")

  2. For i = 1 to 8

  3. Set objInstance1 = objService.Get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")

  4. N1 = objInstance1.PercentProcessorTime

  5. D1 = objInstance1.TimeStamp_Sys100NS

  6. WScript.Sleep(2000)

  7. Set perf_instance2 = objService.get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")

  8. N2 = perf_instance2.PercentProcessorTime

  9. D2 = perf_instance2.TimeStamp_Sys100NS

  10. PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100

  11. WScript.Echo "% Processor Time=" , Round(PercentProcessorTime,2)

  12. Next

This one gets at the data, BUT it's for the TOTAL processor time, I understand the 2 second difference sample and the formula, but no matter what I try in several combinations this is as close as I get.

How can I "echo" this PercentProcessorTime for a specific process? I'm going to go freeeeegin crazy :)

Can anyone help me?
DigiMahn

P.s.
I know I could use the TGML "CODE" tags but the wrapping was just too crazy looking for my anal retentive self :) but here is what I have so far without #3:

set WshShell = CreateObject("WScript.Shell")
Set IE = CreateObject("InternetExplorer.Application")
Set shl = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

Set oShell = CreateObject("Shell.Application")

Set oFolder = oShell.Namespace("C:\WINNT\system32\spool\PRINTERS\")

nCounter = 0

For Each oFile in oFolder.Items

If lcase(Right(oFile.Name,4))=".spl" Then ncounter = nCounter + 1

Next


strMyDocPath = WshShell.SpecialFolders("MyDocuments")
strTempFile = strMyDocPath & "\RenPStat.html"
SetupIE(strTempFile)
Set objDIV = IE.Document.All("MakeMeAnObject")
objDIV.InnerHTML = "Processing Search Script...<BR><FONT SIZE=1>Next process: Locate and Delete --- ---'s invoices"
WScript.Sleep 2000

shl.FindFiles    
WScript.Sleep 1000
WshShell.SendKeys "%M"
WshShell.SendKeys "*.SPL"
WshShell.SendKeys "%L"
WshShell.SendKeys "C:\WINNT\system32\spool\PRINTERS"

WshShell.SendKeys "%C"
WshShell.SendKeys "AMER150 "
SetupIE(strTempFile)
Set objDIV = IE.Document.All("MakeMeAnObject")
objDIV.InnerHTML = "Locating --- --- Invoices...<BR><FONT SIZE=1>Next process: Locate and Delete --- ---'s invoices (in " & (nCounter * .079) & " seconds)"
CommitSearch

WshShell.SendKeys "{TAB 4}"
WshShell.SendKeys "SKIN3910"
CountSPLs
SetupIE(strTempFile)
Set objDIV = IE.Document.All("MakeMeAnObject")
objDIV.InnerHTML = "Locating --- --- Invoices...<BR><FONT SIZE=1>Next process: Locate and Delete --- ---'s invoices (in " & (nCounter * .079) & " seconds)"
CommitSearch



Wscript.Sleep 1000
WshShell.SendKeys "%{F4}"
Wscript.Sleep 500
intReturn = WshShell.Run("C:\TransFlo\rndprint\Stop_svc.bat", 1, TRUE)
WshShell.AppActivate "HP LaserJet 4000 Series PCL - Paused"
WScript.Sleep 500
WshShell.SendKeys "{F5}"
WScript.Sleep 2000
WshShell.SendKeys "%pa"
SetupIE(strTempFile)
Set objDIV = IE.Document.All("MakeMeAnObject")
objDIV.InnerHTML = "Printing Invoices..."
WScript.Quit(1)



Sub SetupIE(File2Load)
IE.Navigate File2Load
IE.ToolBar = False
IE.StatusBar = False
IE.Resizable = False
Do
Loop While IE.Busy
IE.Width = 500
IE.Height = 150
IE.Left = 0
IE.Top = 0
IE.Visible = True
WshShell.AppActivate("Microsoft Internet Explorer")
WshShell.AppActivate("Search Results")
End Sub

Function CountSPLs()
nCounter = 0

For Each oFile in oFolder.Items

If lcase(Right(oFile.Name,4))=".spl" Then ncounter = nCounter + 1

Next

End Function

Sub CommitSearch()
WshShell.SendKeys "~"
WScript.Sleep (79 * nCounter)
WshShell.SendKeys "%ea"
WshShell.SendKeys "+{DEL}"
WshShell.SendKeys "Y"
End Sub
digimahn (IS/IT--Management) (OP)
1 Nov 02 19:04
forgot to mention that I will probably attempt an if then on the processor % usage...

If PercentProcessorTime for "process" > 2 then sleep "x" seconds
If PercentProcessorTime for "process" < 1 then KILL "process"

Thought that may help :)
Helpful Member!  PVilevac (Programmer)
13 Dec 02 20:19
Here's a clue.

The thing I don't like is you've got to use both Win32_Process (for KernalModeTime) and Win32_PerfFormattedData_PerfProc_Process for CPUUsage.  But if you are willing to give up some of the details including KernalModeTime (and command line and ...) you can get everything you need from Win32_PerfFormattedData_PerfProc_Process.

for each Process in GetObject("winmgmts:{impersonationLevel=impersonate}//localhost").ExecQuery("Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process where IDProcess=4092")
   
   WScript.Echo("================================")
   For Each oProperty In Process.Properties_
        WScript.stdout.write vbtab & oProperty.Name & "="
        If IsArray(oProperty) Then
            For iCount = 0 To UBound(oProperty)
                WScript.stdout.write  oProperty.Value(iCount) & ","
            Next
            WScript.StdOut.writeline
        ElseIf IsNull(oProperty) Then
            WScript.stdout.writeline "Property not set"
        Else
            WScript.stdout.writeline oProperty.Value
        End If
    Next
    WScript.Echo(Process.PercentProcessorTime)
  WScript.quit  
next

Email me for a script to generate a full set of stats just like our friend taskman...
PVilevac (Programmer)
16 Dec 02 20:15
Response Part II:

Seems This Works, I believe..


for each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
   WScript.echo Process.name & " " & CPUUSage(Process.Handle) & " %"   
Next


Function CPUUSage( ProcID )

        On Error Resume Next
    
        Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
    
      For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
          N1 = objInstance1.PercentProcessorTime
           D1 = objInstance1.TimeStamp_Sys100NS
           Exit For
       Next

      WScript.Sleep(2000)
   

       For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
           N2 = perf_instance2.PercentProcessorTime
           D2 = perf_instance2.TimeStamp_Sys100NS
           Exit For
       Next

       ' CounterType - PERF_100NSEC_TIMER_INV
       ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
        Nd = (N2 - N1)
        Dd = (D2-D1)
         PercentProcessorTime = ( (Nd/Dd))  * 100
       


    
      CPUUSage = Round(PercentProcessorTime ,0)
    
    
    
End Function
digimahn (IS/IT--Management) (OP)
27 Dec 02 15:17
PVilevac! Thank you!

I implemented a 3rd party tool that I was able to script to/from - I don't like it this way so THANKS once again!!!!
I like to keep things neat and self contained and avoid scripting with anything that is not native to the OS (if possible).

I kinda gave up on these threads...I was looking for a refresher on WSH and printers and there popped up this thread. I don't write that many scripts, but darn if when I do it's a dOOzy like this one!

...I wonder why email notification did not work...hmm.

I can not see a way to email you - maybe I'm just lost :)

Thank you once more...
digimahn (IS/IT--Management) (OP)
27 Dec 02 18:06
Just so others have the info:

for each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process where Name = 'rndprint.exe'")
  WScript.echo Process.name & " " & CPUUSage(Process.Handle) & "%"   
Next

Function CPUUSage( ProcID )
  On Error Resume Next
  Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")

   For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = 'rndprint'")
       N1 = objInstance1.PercentProcessorTime
       D1 = objInstance1.TimeStamp_Sys100NS
     Exit For
   Next

WScript.Sleep(2000)

   For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = 'rndprint'")
       N2 = perf_instance2.PercentProcessorTime
       D2 = perf_instance2.TimeStamp_Sys100NS
     Exit For
   Next

  ' CounterType - PERF_100NSEC_TIMER_INV
  ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
    Nd = (N2 - N1)
    Dd = (D2-D1)
    PercentProcessorTime = ( (Nd/Dd))  * 100

  CPUUSage = Round(PercentProcessorTime ,0)
End Function

The items in bold are the NAME values for my targeted process. Below are two ways to look at all the wonderful items each Class has to offer:

Win32_Process
set wmi = GetObject("winmgmts:root/CIMV2")
wql = "select * from Win32_Process"
set result = wmi.ExecQuery(wql)
for each instance in result
  response = MsgBox(instance.getObjectText_, vbOKCancel)
  if response=vbCancel then exit for
next

Win32_PerfRawData_PerfProc_Process
set wmi = GetObject("winmgmts:root/CIMV2")
wql = "select * from Win32_PerfRawData_PerfProc_Process"
set result = wmi.ExecQuery(wql)
for each instance in result
  response = MsgBox(instance.getObjectText_, vbOKCancel)
  if response=vbCancel then exit for
next


Question:
Since I know EXACTLY what it is I am looking for is it absolutely necessary to enumerate CIMV2 entirely to access this info. This can be time consuming depending on the system involved...is there not a faster way to get at this data?

I sure do hope this helps someone else too!
DigiMahn

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close