Memory Snapshot command

BatchPatch Forums Home Forums BatchPatch Support Forum Memory Snapshot command

  • This topic has 4 replies, 2 voices, and was last updated 4 years ago by doug.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #12614
    TomHuis
    Participant

    Hello,
    I would like to schedule a few monitoring events and want to use the memory snapshot, CPU OS version and Uptime and then also export this as html (prefer csv but not available) so we can add them together after a week of monitoring.
    Problem is that I can’t find the command for wmic memory and also it will overwrite each field if I put them in seperatly so would need one command.
    Can you help or let me know what at least the memory command is to get the total mem and used memory?

    #12620
    doug
    Moderator

    Hello –

    1. You can export the grid to delimited file using “File > Export current grid to delimited file”

    2. The following command will get the RAM numbers you want (in KB):

    WMIC PATH Win32_OperatingSystem GET FreePhysicalMemory, TotalVisibleMemorySize

    3. We will consider adding the built-in ‘Get RAM usage snapshot’ to the job queue and/or task scheduler so that it can be scheduled to run without a custom command.

    4. Below is a script that gets all of the information you asked about. You can incorporate it into BP as a “local command” with the following syntax. BP will pass the target computer name from the ‘Host’ column into the script by using ‘$computer’ as shown here:

    Create a local command that looks like this:

    cscript "C:\SomeFolderOnBPComputer\Scripts\ScriptSample.vbs" $computer

    Here is the actual script. Put it in a text editor and save it with a .vbs extension. Modify it as desired to get the results formatted in your preferred way:

    'Gets the number of CPU Sockets, Cores, Logical Processors, OSVersion, LastBootUpTime etc.  Cocobolo Software LLC December 2020.
    'usage: cscript.exe script.vbs COMPUTERNAME
    
    strComputer = WScript.Arguments(0)
     
    on error resume next
    Err.Clear
     
    'strComputer = TextBox.value 'InputBox("Enter the Computer Name or IP address")
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
     
    OSVersionCaption = "" 
     
    'Get OS Version for CPU info
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    		For Each objOperatingSystem in colOperatingSystems
    			OSVersionCaption = objOperatingSystem.Caption
    		Next
     
    'Get Processor Info (note: it can take a long time to pull info from this class)	
    Sockets = 0	
    Cores = 0
    LogicalProcessors = 0
    WMISupport = 1
    ProcessorName = ""
     
    If InStr(OSVersionCaption,"2003") or InStr(OSVersionCaption,"XP") or InStr(OSVersionCaption,"2000") Then
    	WMISupport = 0
    End If
     
    Set colProc = objWMIService.ExecQuery("Select * from Win32_Processor")
    	For Each objProc in colProc 
    		ProcessorName = objProc.Name
    		Sockets = Sockets + 1
    		Cores = Cores + objProc.NumberOfCores
    		If Err.Number <> 0 Then
    			Cores = Err.Description
    			Err.Clear
    		End If
    		LogicalProcessors = LogicalProcessors + objProc.NumberOfLogicalProcessors
    	Next
     
    If WMISupport = 0 Then
    	LogicalProcessors = Sockets
    	Sockets = "Property is not supported for this OS"
    	Cores = "Property is not supported for this OS"
    End If
    
    OSVersion = ""
    LastBootUpTime = ""
    FreePhysicalMemory = ""
    TotalVisibleMemorySize =""
    
    Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    	For Each objOS in colOS
    		OSVersion = objOS.Version
    		LastBootUpTime = objOS.LastBootUpTime
    		FreePhysicalMemoryKB = objOS.FreePhysicalMemory
    		TotalVisibleMemorySizeKB = objOS.TotalVisibleMemorySize
    	Next
     
    wscript.echo("CPU: " & ProcessorName & vbCrLf & "Sockets: " & Sockets & vbCrLf & "Cores: " & Cores & vbCrLf & "Logical Procs: " & LogicalProcessors & vbCrLf & vbCrLf & "OSVersion: " & OSVersionCaption & " " & OSVersion & vbCrLf & "LastBootUpTime: " & LastBootUpTime & vbCrLf & "FreePhysicalMemoryKB: " & FreePhysicalMemoryKB & vbCrLf & "TotalVisibleMemorySizeKB: " & TotalVisibleMemorySizeKB)
    #12622
    TomHuis
    Participant

    Thanks a lot Doug!
    I tried to find the command to show what is used Ram versus what is available, do you have this as well? If not then not a big deal as I can do the math as well 🙂

    #12623
    TomHuis
    Participant

    One more thing, see output below when running localhost and on a remote machine. Not a big deal as I can also adjust to run it locally instead but do you know what could be the issue?
    Also, can the format be adjusted for memory in MB’s and the time in dd/mm/yy 00:00 ?

    C:\Batchpatch\Scripts>cscript Get-VM-usage.vbs localhost
    Microsoft (R) Windows Script Host Version 5.812
    Copyright (C) Microsoft Corporation. All rights reserved.

    CPU: Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz
    Sockets: 2
    Cores: 2
    Logical Procs: 2

    OSVersion: Microsoft Windows Server 2016 Datacenter 10.0.14393
    LastBootUpTime: 20201113132422.921456+060
    FreePhysicalMemoryKB: 2311396
    TotalVisibleMemorySizeKB: 4193780

    C:\Batchpatch\Scripts>cscript Get-VM-usage.vbs hmw37-gc-w002.hmw37.nl
    Microsoft (R) Windows Script Host Version 5.812
    Copyright (C) Microsoft Corporation. All rights reserved.

    CPU:
    Sockets: 1
    Cores: Object required
    Logical Procs: 0

    OSVersion:
    LastBootUpTime:
    FreePhysicalMemoryKB:
    TotalVisibleMemorySizeKB:

    C:\Batchpatch\Scripts>

    #12626
    doug
    Moderator

    The script generally works for most computers (for example it works on all of the computers we have here without any errors/issues), but if you have a particular machine where the script is not running successfully, then there is something specific to that machine that is causing the issue. You would have to setup some error handling in the script to get to the root of why that particular machine isn’t completing successfully. It could be a transient error with WMI on that computer, and restarting WMI or that computer could possibly resolve it. But it might also be some kind of corruption with the WMI repository or something else altogether. I can’t say for sure.

    I provided the script to you as a template to show you an example of the kind of thing that is possible. However, you should customize it for your particular needs/desires:

    To get the amount of RAM in use you have to do the calculation TotalVisibleMemorySizeKB – FreePhysicalMemoryKB. You can add this calculation directly in the script.

    To get megabyte from kilobyte, you would take the kilobyte value and divide by 1024 or 1000, depending on how you define a MB (as either 1000KB or 1024KB). You can add this calculation directly in the script.

    To change the format of the date-time, you’ll need to do some processing of that date-time result, which can also be added to the script. Here is an example: https://stackoverflow.com/questions/29535638/vbscript-how-to-convert-a-date-into-days-and-time

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.