VBScript to monitor total CPU usage in percentage

Answered VBScript to monitor total CPU usage in percentage

  • Tuesday, November 20, 2012 11:21 AM
     
     

    Is there any VBScript to monitor Total CPU usage in percentage as we see in Task Manager, I have got couple of scripts, but they gives only CPU usage per process or processor time per process.

    Any help would be appreciated.

    Thanks,

    Surendar

     


    Thanks, Surendar

All Replies

  • Tuesday, November 20, 2012 11:47 AM
     
      Has Code

    Hi, this will do the trick, it will display the percentages of all individual CPUs and the average percentage:

    Set objWMIService = GetObject("winmgmts:\\localhost\root\CIMV2") 
    Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48) 
    For Each Item in CPUInfo 
        Wscript.Echo "Win32_PerfFormattedData_PerfOS_Processor instance"
        Wscript.Echo "PercentProcessorTime: " & Item.PercentProcessorTime
    Next
    


    Jaap Brasser
    http://www.jaapbrasser.com

  • Tuesday, November 20, 2012 12:51 PM
     
     

    Hi Jaap,

    Thanks, I will test the script, also I want the output to be written in a txt file, any help.


    Thanks, Surendar

  • Wednesday, November 21, 2012 8:17 AM
     
     Answered Has Code

    Here is a version of the script that both writes to log as well as to the console:

    Set objWMIService = GetObject("winmgmts:\\localhost\root\CIMV2") 
    Set CPUInfo = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48) 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objLogFile = objFSO.OpenTextFile("C:\Log.log", 8, True, 0)
    For Each Item in CPUInfo 
        Wscript.Echo "Win32_PerfFormattedData_PerfOS_Processor instance"
        objLogFile.WriteLine "Win32_PerfFormattedData_PerfOS_Processor instance"
        Wscript.Echo "PercentProcessorTime: " & Item.PercentProcessorTime
        objLogFile.WriteLine "PercentProcessorTime: " & Item.PercentProcessorTime
    Next

    An alternative for any script output to be written to file would be to use the redirection. For example:

    cscript anyscript.vbs >> logfile.txt


    Jaap Brasser
    http://www.jaapbrasser.com

  • Thursday, November 22, 2012 3:26 PM
     
     

    Hi Jaap,

    I am getting below error while executing this script in Windows 2003 server,

    C:\tmp>cscript VBScript.vbs
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001

    C:\tmp\VBScript.vbs(5, 1) (null): 0x80041003


    Thanks, Surendar

  • Monday, November 26, 2012 12:48 PM
     
     
    Ok, I had to have a good look to be able to find a 2003 server to test this script on. But the script works fine here, so I am not sure what I can do to help you. Can you try and execute this script on another server and verify if it works there?

    Jaap Brasser
    http://www.jaapbrasser.com