none
Script to get memmory and cpu usage

    Question

  • Our company uses monitoring tools to alert on CPU and memory but I am trying to figure out a script that would give me the results of CPU and memory are being used when I run it?   Any direction would be helpful in that I am new to scripting and this is a windows 2000 server.


    Scott
    Friday, October 10, 2008 4:28 PM

Answers

  • Hi,

    Please try this:

    1 On Error Resume Next 
    2  
    3 strComputer = "." 
    4 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")  
    5  
    6 'Get CPU Usage  
    7 myQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'" 
    8 For Each objItem in objWMIService.ExecQuery(myQuery)  
    9    ProcTime = "Processor time " & objItem.PercentProcessorTime & "  %" 
    10 next  
    11  
    12 'Get Memory Information  
    13 myQuery = "select FreeVirtualMemory,FreePhysicalMemory from Win32_OperatingSystem" 
    14 for each objItem in objWMIService.ExecQuery(myQuery)  
    15     for each oProperty in objItem.Properties_  
    16         if oProperty.Name = "VirtualMemFreeMemory" then  
    17             VirtualMemFree = oProperty.Value  
    18         elseif oProperty.Name = "PhMemFreeMemory" then  
    19             PhMemFree = oProperty.Value  
    20         end if  
    21     next  
    22 next  
    23  
    24 myQuery = "select TotalVirtualMemory,TotalPhysicalMemory from Win32_LogicalMemoryConfiguration" 
    25 for each objItem in objWMIService.ExecQuery(myQuery)  
    26     for each oProperty in objItem.Properties_  
    27         if oProperty.Name = "TotalVirtualMemory" then  
    28             VirtualTotal = oProperty.Value  
    29         elseif oProperty.Name = "TotalPhysicalMemory" then  
    30             PhysicalTotal = oProperty.Value  
    31         end if  
    32     next  
    33 next  
    34  
    35 VirtualUsed = 100 - (100 * (VirtualMemFree / VirtualTotal))  
    36 PhysicalUsed = 100 - (100 * (PhMemFree / PhysicalTotal))  
    37  
    38 wscript.echo Date() & " " & Time()  
    39 wscript.echo "Processor Time : " & ProcTime  
    40 wscript.echo "Percent Used Virutal Memory" & VirtualUsed  
    41 wscript.echo "Percent Used Physical Memory" & PhysicalUsed 

    Regards,

    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    Bytes & Badz : http://badzmanaois.blogspot.com
    Saturday, October 11, 2008 11:00 AM

All replies

  • Hi,

    Please try this:

    1 On Error Resume Next 
    2  
    3 strComputer = "." 
    4 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")  
    5  
    6 'Get CPU Usage  
    7 myQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'" 
    8 For Each objItem in objWMIService.ExecQuery(myQuery)  
    9    ProcTime = "Processor time " & objItem.PercentProcessorTime & "  %" 
    10 next  
    11  
    12 'Get Memory Information  
    13 myQuery = "select FreeVirtualMemory,FreePhysicalMemory from Win32_OperatingSystem" 
    14 for each objItem in objWMIService.ExecQuery(myQuery)  
    15     for each oProperty in objItem.Properties_  
    16         if oProperty.Name = "VirtualMemFreeMemory" then  
    17             VirtualMemFree = oProperty.Value  
    18         elseif oProperty.Name = "PhMemFreeMemory" then  
    19             PhMemFree = oProperty.Value  
    20         end if  
    21     next  
    22 next  
    23  
    24 myQuery = "select TotalVirtualMemory,TotalPhysicalMemory from Win32_LogicalMemoryConfiguration" 
    25 for each objItem in objWMIService.ExecQuery(myQuery)  
    26     for each oProperty in objItem.Properties_  
    27         if oProperty.Name = "TotalVirtualMemory" then  
    28             VirtualTotal = oProperty.Value  
    29         elseif oProperty.Name = "TotalPhysicalMemory" then  
    30             PhysicalTotal = oProperty.Value  
    31         end if  
    32     next  
    33 next  
    34  
    35 VirtualUsed = 100 - (100 * (VirtualMemFree / VirtualTotal))  
    36 PhysicalUsed = 100 - (100 * (PhMemFree / PhysicalTotal))  
    37  
    38 wscript.echo Date() & " " & Time()  
    39 wscript.echo "Processor Time : " & ProcTime  
    40 wscript.echo "Percent Used Virutal Memory" & VirtualUsed  
    41 wscript.echo "Percent Used Physical Memory" & PhysicalUsed 

    Regards,

    Salvador Manaois III
    MCSE MCSA CEH MCITP | Enterprise/Server Admin
    Bytes & Badz : http://badzmanaois.blogspot.com
    Saturday, October 11, 2008 11:00 AM
  • Thank you so much...
    I am having issues running it on windows 2000 server it just returns the following:
    10/13/2008 9:52:24 AM
    Processor Time :
    Percent Used Virutal Memory100
    Percent Used Physical Memory100
    ??

    I did test it locally on my xp test laptop and it ran correctly..
    S


    Scott
    Monday, October 13, 2008 3:11 PM