Improve Task Manager script (no image recognition, etc.)

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Improve Task Manager script (no image recognition, etc.)

Post by Warren » Sat Oct 21, 2017 11:36 pm

I've got a working script to open the task manager, show details, force it on top, resize it, and scroll to show only the memory and cpu columns, but I'm trying to develop some best practices, so there are a few things about it I'd like to analyze.

Here is current script:

Code: Select all

//Open Task Manager, and show more details if not already showing
RunProgram>C:\Windows\system32\taskmgr.exe
WaitWindowOpen>Task Manager
SetFocus>Task Manager
Wait>3
UIClick>Task Manager,More details
Wait>3

//Open options, check "Always on top" if unchecked.  If checked, close options
UIClick>Task Manager,Options
GoSub>waitImage,alwaysOnTopUnchecked,3
IF>WSI_RESULT=1
  UIClick>Task Manager,Always on top
ELSE
  UICLICK>Task Manager,Options
ENDIF
wait>1

//Move and resize window
MoveWindow>Task Manager,0,0
ResizeWindow>Task Manager,163,260

//Scroll full right
GetWindowHandle>Task Manager,hWndParent
FindObject>hWndParent,ScrollBar,,6,hWnd,X1,Y1,X2,Y2,result
ObjectSendKeys>hWnd,VK34
ObjectSendKeys>hWnd,VK34
ObjectSendKeys>hWnd,VK34
There are 4 areas I'm interested in improving.

1) The first item I'd like to improve is that I'm using a subroutine I created called waitImage (not elaborated since I intend to ditch it here) that waits for an image to appear for a user specified number of seconds, then WSI_RESULT will return if it is found. I'm trying wherever possible to use keyboard commands and UI element commands rather than image recognition or ocr tools, so I'd like to see if that's possible here.

Problem is that "Always on top" in Options may or may not already be checked, and as far as I can tell, the checkbox next to it shows nothing for value set, and doesn't respond to me setting the value, so I can click it, but I need to know first if I SHOULD click it.... without looking for an image of it clicked or not. I need some way of either checking the current value, or setting the value.

2) With the mess at the end, I'm basically just trying to scroll full right on the horizontal scrollbar, but I haven't been able to find a keyboard command that does that, so instead I've got this mess of scrolling accross in 3 increments. I've tried a few things involving pagedown or end, but they haven't worked so far.

3) I'm wondering if it's a better practice for any particular reason to launch the Task Manager with the RunProgram> command as above, or to use the keybard Ctrl>Shift>Esc command. Is it ALWAYS better to use key commands, or RunProgram an exception? Is it an issue that the RunProgram command relies on a filepath? Is it possible that the filepath is different on some version of Windows other than 2012r2?

4) It's not in the above script yet, but the other thing I'm trying to do is to sort by Memory in descending order. In this case, I can see that when it is sorted this way, the Memory element does have a set value indicating the descending order, but clicking on it does nothing, and I have so far been unsuccessful setting the value if it needs setting.

thx

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Improve Task Manager script (no image recognition, etc.)

Post by Marcus Tettmar » Sun Oct 22, 2017 9:07 am

Wondering why you are wanting to automate task manager? You can get all the details of task manager programmatically.

Not sure I can answer all your points.

1. Maybe use the API function SetWindowPos instead to make it on top anyway.
2. Can't you just set the width of the window with ResizeWindow
3. I always prefer to RunProgram than use keystrokes. Task Manager will be in the path on all PCs as it's part of Windows. Obviously for other apps you will know the path or can determine it. But if you want the path and don't want to hard code the location of Windows use the environment variables or registry to find the path or SYS_DIR etc.
4. Again, any reason why you wouldn't just use VBScript to get a list of processes running and their memory consumption, then end up with a sorted array or similar?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Sun Oct 22, 2017 7:13 pm

Marcus Tettmar wrote:Wondering why you are wanting to automate task manager? You can get all the details of task manager programmatically.

Not sure I can answer all your points.

1. Maybe use the API function SetWindowPos instead to make it on top anyway.
2. Can't you just set the width of the window with ResizeWindow
3. I always prefer to RunProgram than use keystrokes. Task Manager will be in the path on all PCs as it's part of Windows. Obviously for other apps you will know the path or can determine it. But if you want the path and don't want to hard code the location of Windows use the environment variables or registry to find the path or SYS_DIR etc.
4. Again, any reason why you wouldn't just use VBScript to get a list of processes running and their memory consumption, then end up with a sorted array or similar?
1. I had come across and read a bit about SetWindowPos, but got the impression it was a constant loop that may be adding unnecessary cpu cycles, ets. I could be wrong about that.

2. I've already resized the window. That's the problem. it's a tiny window now just big enough to show the bits I need, but needs to be scrolled to be showing the correct columns.

3. OK, and I'll read up on some of that.

4 . TM might not be the best solution. I'm not sure. I've got 100 vps currently, and looking to at least triple that over the next few months as the scripts take over more duties. I'm just using TM floating on top off in the corner as a quick visual reference to make sure ram and cpu are ok. If there's a better way to monitor that in realtime, I'd be happy to look into it. The options I checked out that give ram and cpu totals visually in the taskbar were all way too small to read at a quick glance. I'm sure I'll eventually script out the need to even check visually, but until then looking for a quick overview of each machine's performance in realtime.

Because they are shared core vps, I need to keep an eye on cpu, and I need to watch ram as well to avoid disk swaps if I run out of raam whcih can lead to my provider shutting down the machines for extended read/write bursts violating I/O guidelines in TOS.

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Improve Task Manager script (no image recognition, etc.)

Post by Marcus Tettmar » Sun Oct 22, 2017 8:53 pm

Ok, fair enough. So I think what I would do is create my own dialog which shows just the info I want about the processes I want. Create a custom dialog with a listbox or a stringgrid. Get a list of processes and the details of each that you want programmaticaly (use VBScript and query Win32_Process) and then sort the results the way you want and populate your listbox/stringgrid accordingly. That way you're not trying to manipulate Task Manager and you have more control over the appearance because it is your dialog.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Mon Oct 23, 2017 1:28 am

Another thought would be to create a script to monitor your processes and throw up a message when your predefined limits are met rather than watching Task Manager forever.

I think I have a script at work that puts process CPU info into a dialog. I'll see if I can find it tomorrow. I think it uses the vbscript posted HERE But I don't think I have anything that monitors RAM.

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Mon Oct 23, 2017 7:57 pm

Appreciated. The ram is a little easier because it generally only goes up until something is closed. Some of the sites I'm on don't handle ram well, and leak over time. If it's over 85%, something is likely wrong, and time to reboot the system which then retriggers the script which relaunches the browsers.

The CPU is a bit trickier because it fluctuates wildly and will always spike. I'll look it up the exact specs, but I believe the TOS for my provider is that it can't run for more than a certain time interval (minutes) without dipping below 50%.

I really want to keep everything as lightweight as possible, though, so checking less frequently is fine, and while I would ideally want it to hover above in dead space (taskbar ideally since the rest of the screen is filled), if staying on top is resource intensive, it's not worth it. I don't have a lot of headroom. One of my favorite things about MS is that I can run it lean.

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Mon Oct 23, 2017 9:05 pm

Try this:

Closing the dialog will close the script. Set memory and cpu limits in the variables at the top of the script. If the limits are surpassed the text turns red.

For some reason the memory vbscript is leaking memory (irony?) I'll see if I can find a way to fix it.

Code: Select all

Let>RAM_Limit=85
Let>CPU_Limit=50

Dialog>Dialog1
object Dialog1: TForm
  Left = 344
  Top = 178
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'Memory and CPU'
  ClientHeight = 53
  ClientWidth = 422
  Color = 16777200
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 0
    Width = 96
    Height = 16
    Caption = 'Total Memory'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 120
    Top = 0
    Width = 104
    Height = 16
    Caption = 'Memory in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 248
    Top = 0
    Width = 164
    Height = 16
    Caption = 'CPU Percentage in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 8
    Top = 24
    Width = 48
    Height = 16
    Caption = 'First run sometimes takes a while.  Be patient...'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label5: TLabel
    Left = 120
    Top = 24
    Width = 48
    Height = 16
    Caption = ''
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label6: TLabel
    Left = 248
    Top = 24
    Width = 48
    Height = 16
    Caption = ''
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnClose,Quit
Let>WIN_USEHANDLE=1
  MoveWindow>Dialog1.Handle,0,0
Let>WIN_USEHANDLE=0
SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,111111
Show>dialog1

SRT>Quit
  Exit>0
END>Quit

VBSTART
Function CPUUSage(ProcName)
  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 = '" & ProcName & "'")
       N1 = objInstance1.PercentProcessorTime
       D1 = objInstance1.TimeStamp_Sys100NS
     Exit For
   Next
WScript.Sleep(1000)
   For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = '" & ProcName & "'")
       N2 = perf_instance2.PercentProcessorTime
       D2 = perf_instance2.TimeStamp_Sys100NS
     Exit For
   Next
    Nd = (N2 - N1)
    Dd = (D2 - D1)
    PercentProcessorTime = ((Nd/Dd)/2) * 100
  CPUUSage = Round(PercentProcessorTime ,0)
End Function

Function RAMUsage
  strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
  ANS = (round ((objComputer.TotalPhysicalMemory / 1024 ^ 3),2))
Next

set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Memory").objectSet
objRefresher.Refresh

    For Each objItem in colItems
        A1 = (round ((objItem.AvailableMBytes / 1024),2))
        
        objRefresher.Refresh
        RAMUsage = ANS & vbcrlf & A1
    Next
End Function
VBEND



Label>Start
  GetDate>today
  GetTime>now

  VBEval>CPUUSage("Idle"),CPUres
    Let>CPUres={100-(%CPUres%/2)}
    If>{%CPUres%>%CPU_Limit%}
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,0
    EndIf

  VBEval>RAMUsage,RAMres
    Separate>RAMres,crlf,RAM
    SetDialogProperty>Dialog1,,Caption,Memory and CPU %today% %now%
    SetDialogProperty>Dialog1,Label6,Caption,CPUres
    Let>RAM_Percent={(%RAM_2%/%RAM_1%)*100}
    If>{((%RAM_2%/%RAM_1%)*100)>%RAM_Limit%}
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,0
    EndIf
    SetDialogProperty>Dialog1,Label5,Caption,RAM_2
    SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,0
    SetDialogProperty>Dialog1,Label4,Caption,RAM_1
    
  
  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog1.Handle,DiaX,DiaY
    MoveWindow>Dialog1.Handle,DiaX,DiaY
  Let>WIN_USEHANDLE=0
  Wait>0.01
Goto>Start

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Mon Oct 23, 2017 10:23 pm

The percentages were inverses (showing available instead of used.

I boiled it down to what I'm looking for which is just whole number ram and cpu totals that are color coded and visible from space. It probably still needs some cleaning up as I was just concentrating on dialing in the bits that didn't fit my needs.

Code: Select all

Let>RAM_Red=85
Let>RAM_YELLOW=50
Let>CPU_Red=60
Let>CPU_YELLOW=40



Dialog>Dialog1
object Dialog1: TForm
  Left = 0
  Top = 0
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = ''
  ClientHeight = 40
  ClientWidth = 350
  Color = 0
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -40
  Font.Name = 'MS Sans Serif'
  Font.Style = [fsBold]
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 40
 
  object Label2: TLabel
    Left = 190
    Top = 0
    Width = 100
    Height = 40
    Caption = 'RAM'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 0
    Font.Height = -36
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  
  object Label3: TLabel
    Left = 20
    Top = 0
    Width = 100
    Height = 40
    Caption = 'CPU'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 0
    Font.Height = -36
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
 
  object Label5: TLabel
    Left = 290
    Top = 0
    Width = 100
    Height = 40
    Caption = ''
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 111111
    Font.Height = -36
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  
  object Label6: TLabel
    Left = 120
    Top = 0
    Width = 100
    Height = 40
    Caption = ''
    Font.Charset = DEFAULT_CHARSET
    Font.Color = 111111
    Font.Height = -36
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end
EndDialog>Dialog1








AddDialogHandler>Dialog1,,OnClose,Quit
Let>WIN_USEHANDLE=1
  MoveWindow>Dialog1.Handle,800,0
Let>WIN_USEHANDLE=0
Show>dialog1

SRT>Quit
  Exit>0
END>Quit

VBSTART
Function CPUUSage(ProcName)
  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 = '" & ProcName & "'")
       N1 = objInstance1.PercentProcessorTime
       D1 = objInstance1.TimeStamp_Sys100NS
     Exit For
   Next
WScript.Sleep(1000)
   For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = '" & ProcName & "'")
       N2 = perf_instance2.PercentProcessorTime
       D2 = perf_instance2.TimeStamp_Sys100NS
     Exit For
   Next
    Nd = (N2 - N1)
    Dd = (D2 - D1)
    PercentProcessorTime = ((Nd/Dd)/2) * 100
  CPUUSage = Round(PercentProcessorTime ,3)
End Function

Function RAMUsage
  strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
  ANS = (round ((objComputer.TotalPhysicalMemory / 1024 ^ 3),3))
Next

set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Memory").objectSet
objRefresher.Refresh

    For Each objItem in colItems
        A1 = (round ((objItem.AvailableMBytes / 1024),3))
       
        objRefresher.Refresh
        RAMUsage = ANS & vbcrlf & A1
    Next
End Function
VBEND



Label>Start
  //GetDate>today
  //GetTime>now

  VBEval>CPUUSage("Idle"),CPUres
    Let>CPU_Percent={100-%CPUres%}
    Let>CPU_Percent={round(%CPU_Percent%)}
    If>{%CPU_Percent%>%CPU_Red%}
      Let>cpuColor=255
    Else
      If>{%CPUres%>%CPU_Yellow%}
        Let>cpuColor=111100
      Else
        Let>cpuColor=111111
      Endif
    EndIf
    SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,30,1,cpuColor
    SetDialogObjectFont>Dialog1,Label3,MS Sans Serif,30,1,cpuColor
    SetDialogProperty>Dialog1,Label6,Caption,CPU_Percent

  VBEval>RAMUsage,RAMres
    Separate>RAMres,crlf,RAM
    
    Let>RAM_Percent={100-((%RAM_2%/%RAM_1%)*100)}
    Let>RAM_Percent={Round(%RAM_Percent%)}
    
    If>{%RAM_Percent%>%RAM_Red%}
      Let>ramColor=255
    Else
      If>{%RAM_Percent%>%RAM_Yellow%}
        Let>ramColor=111100
      Else
        Let>ramColor=111111
    EndIf
    SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,30,1,ramColor
    SetDialogObjectFont>Dialog1,Label2,MS Sans Serif,30,1,ramColor
    SetDialogProperty>Dialog1,Label5,Caption,RAM_Percent
    
   
 
  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog1.Handle,DiaX,DiaY
    MoveWindow>Dialog1.Handle,DiaX,DiaY
  Let>WIN_USEHANDLE=0
  Wait>0.01
Goto>Start
A few remaining issues and questions:

1) The CPU always seems to be low... at least a few percentage points lower than what TM shows. I thought this might be a rounding error as it adds everything up, so tried increasing decimals in rounding, and a few other things, but it still always reads a bit low compared to TM. Not sure if this is a real issue or not.

2) The look of it is basically what I want now, but ideally, it would just be an overlay 2/3 of the way to the right or so on the taskbar since that's the only available real estate I've got. I changed the Y value to 0 in the script above so it would be visible on anyone's screen running the script. I can get it to persist above the taskbar, bu the window chrome sticks out. I played around a bit with the dialog creator, but don't see a way to create a chromeless window. Is such a thing possible? If not, is it possible to just do what I really want and print it directly on the taskbar?

3) The part I really can't wrap my head around yet is that every script I've encountered thus far is sequential. The whole point of this is to monitor what's happening as another script is running. So... does that mean I need to have two separate scripts running in parallel, or is there a way (just don't know what terms to look up) to essentially embed this in the main script and give it a command to run in parallel to the other portions of the script?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Tue Oct 24, 2017 6:14 pm

Still have not fixed the memory leak but I did find time to look it up and confirmed that "SWbemRefresher" does have an issue. Maybe someone skilled in VBScript can find a way to capture memory usage that does not use SWbemRefresher and does not consume (leak) memory.

1) If its a difference between 23 and 25 or 51 vs 49 I would not be concerned. The likelihood of ever exactly matching TM for any length of time is low. TM and this script capture cpu at a moment and capture cpu at a second moment then make a comparison to see what the percentage of usage is over that period of time. Our slice of time is one second plus processing time, TM will most likely capture a different slice length and probably process the result much faster, this could result in a small difference. Since the cpu usage is constantly changing the exact percentage may be slightly different because we and TM will have taken different slices. I first wrote this cpu thing 3 years ago and my memory is a little hazy but I think there was something about the number of processors that can influence the results. so if TM says 25 and you're getting for example 50 you'll need to do the math and cut the script percentage in half.

2) Not familiar with the term "window chrome". We can do a lot of things with dialogs including removing the window entirely. Print the text on the desktop or on any other window or atop anything that happens to be on the screen. Gets more complicated and will not be as lean.

3) Macro Scheduler scripts execute one line after another. My understanding is that's one of the significant differences between scripting and OOP for example. "Compiled" scripts are not truly compiled in the sense of a programming language compile. Just in the sense that the compiled script can be run independently. In any case, yes, this script, compiled or uncompiled, would be run along side rather than as a part of other scripts. You can run it from another script using the ExecuteFile> function. Just make sure you build in a method to detect it is already running so you don't run it more than once, and a way to programmatically close it. If you'd like I can elaborate on how I would do this. Could get lengthy, I'm wordy by nature. :shock:

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Tue Oct 24, 2017 7:26 pm

I'll look into the parallel part when I get to that point. Just trying to get a concept of where the script fits in to the bigger picture at the moment.

"Chrome" is the term YouTube uses to describe everything you see but the video itself, so a "Chromeless" player is one that has no visible windowframe, player controls, etc.

In this case, the "chrome" would be the windowframe and it's components like logo, red close X, etc.

Specifically in this case, I'd prefer to just have the black rectangle with no windowframe at all so I can fit it over the taskbar with nothing sticking out (no frame). If that option was contained in the dialog builder, I missed it.

Not really concerned about the cpu reading. It's pretty consistently 2-5 below TM reading.

As you may have guessed by the giant color coded text, I'm more just trying to get a quick visual ballpark as I scan across thumbnails of 100 machines at once. It would actually be fine if it only took a reading every few seconds or so if that helps on resources.

At the moment, I just need something that works visually, I'll worry about how to have it measure over a given time period to decide whether to trigger something once I've got the rest of the system working.

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Tue Oct 24, 2017 8:29 pm

The eliminate the border, go to Dialog Designer, scroll down to "BorderStyle" and from the borderstyle menu select "bsNone". Save the dialog, save the script. Does that give you the look you want? You may need to resize the dialog to make it fit the taskbar area.

Once the border is removed the dialog cannot be moved with the mouse unless you add some code to move it by picking in the client area. I've post that on the forum someplace. I'll find it if you need it. Also to close the script you'll either need to add code for closing or you can select the dialog then press Alt + F4 which will close the dialog and thus close the script.

Also, if you're looking at 100 machines perhaps instead of a dialog per machine you could look at one master dialog with an excel like grid containing data from all the machines. Do you have a shared drive?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Tue Oct 24, 2017 9:52 pm

Here's a version written to find the info via wmic. Got the memory turned around so it matches the label and took off the border. You'll have to place it where you want it.

Also, no memory leak with wmic.

Code: Select all

Let>vRAM_Limit=85
Let>vCPU_Limit=50

Dialog>Dialog1
object Dialog1: TForm
  Left = 284
  Top = 178
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsNone
  Caption = 'Memory and CPU'
  ClientHeight = 53
  ClientWidth = 422
  Color = 16777200
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 0
    Width = 96
    Height = 16
    Caption = 'Total Memory'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 120
    Top = 0
    Width = 104
    Height = 16
    Caption = 'Memory in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 248
    Top = 0
    Width = 164
    Height = 16
    Caption = 'CPU Percentage in Use'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 8
    Top = 24
    Width = 323
    Height = 16
    Caption = 'First run sometimes takes a while.  Be patient...'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label5: TLabel
    Left = 120
    Top = 24
    Width = 5
    Height = 16
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label6: TLabel
    Left = 248
    Top = 24
    Width = 5
    Height = 16
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -13
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnClose,Quit
Let>WIN_USEHANDLE=1
  MoveWindow>Dialog1.Handle,0,0
Let>WIN_USEHANDLE=0
SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,111111
Show>dialog1

Let>RP_Wait=1
Let>RP_Windowmode=0
//Let>RP_Admin=1

SRT>Quit
  Exit>0
END>Quit

RunProgram>cmd /c wmic computersystem get TotalPhysicalMemory > %temp_dir%CPUandMEM.txt
ReadLn>%temp_dir%CPUandMEM.txt,2,vTotalMem
Trim>vTotalMem,vTotalMem
Let>vTotalMem={(round((%vTotalMem%/1024^3)*100)/100)}

SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,0
SetDialogProperty>Dialog1,Label4,Caption,vTotalMem

Label>Start
  GetDate>today
  GetTime>now

  RunProgram>cmd /c wmic cpu get loadpercentage > %temp_dir%CPUandMEM.txt
  RunProgram>cmd /c wmic OS get FreePhysicalMemory /Value >> %temp_dir%CPUandMEM.txt
  
  ReadLn>%temp_dir%CPUandMEM.txt,2,vCPUres
  Trim>vCPUres,vCPUres
  
    If>{%vCPUres%>%vCPU_Limit%}
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label6,MS Sans Serif,11,1,0
    EndIf
  
  ReadLn>%temp_dir%CPUandMEM.txt,5,vMEMres
  Trim>vMEMres,vMEMres
  StringReplace>vMEMres,FreePhysicalMemory=,,vMEMres
  Let>vMEMres={(round((%vMEMres%/1024^2)*100)/100)}
  Let>vMEMres={%vTotalMem%-%vMEMres%}
  
    SetDialogProperty>Dialog1,,Caption,Memory and CPU %today% %now%
    SetDialogProperty>Dialog1,Label6,Caption,vCPUres

    If>{((%vMEMres%/%vTotalMem%)*100)>%vRAM_Limit%}
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,255
    Else
      SetDialogObjectFont>Dialog1,Label5,MS Sans Serif,11,1,0
    EndIf
    SetDialogProperty>Dialog1,Label5,Caption,vMEMres
    SetDialogObjectFont>Dialog1,Label4,MS Sans Serif,11,1,0
    SetDialogProperty>Dialog1,Label4,Caption,vTotalMem
    
  
  Let>WIN_USEHANDLE=1
    GetWindowPos>Dialog1.Handle,DiaX,DiaY
    MoveWindow>Dialog1.Handle,DiaX,DiaY
  Let>WIN_USEHANDLE=0
  Wait>0.01
Goto>Start

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Tue Oct 24, 2017 10:01 pm

I don't have a shared drive currently.

I'm entering a transition. I've been working with 100 machines personally with no automation except imacros browser addon. It's gotten to the point where it's no longer practical, and I'm clear that I ultimately need a centrally managed end-to-end workflow. I'd have triple the machines right now if the automation was already in place.

In the meantime as I learn the tools, however, I'm piecing together scripts on a test machine for exe rollout. My RDP client has an "overview" mode where I can scroll across all the machines and see a moving snapshot of them all quickly. With a big enough colored readout, I can see if any of them are having resources issues in under a minute while simultaneously seeing if anything else looks out of order. I see it as a stopgap, but one step at a time.

I haven't gotten into the remote control yet or explored tools for central control over all the virtual machines. Still waiting for word back on some presale Q's I asked about that. (I'm thinking my contact form may have gotten lost in the shuffle) I'm assuming that's the direction I'm headed once I work out the scripts each machine should be executing.

In the meantime, I've just been testing compiled exe's on a couple machines via dropbox. Yes, though, some kind of shared drive will be necessary as I will have central list of data I will need to draw upon once I get past automating individual browser profiles, and to creating and managing them.

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Improve Task Manager script (no image recognition, etc.)

Post by JRL » Tue Oct 24, 2017 10:05 pm

Note that I posted a new script above. Sometimes second posting by the same user gets overlooked.

Warren
Pro Scripter
Posts: 83
Joined: Sun Oct 08, 2017 11:57 pm

Re: Improve Task Manager script (no image recognition, etc.)

Post by Warren » Tue Oct 24, 2017 10:28 pm

JRL wrote:Here's a version written to find the info via wmic. Got the memory turned around so it matches the label and took off the border. You'll have to place it where you want it.

Also, no memory leak with wmic.
On my Windows 2012 r2 KVM VPS, CPU reads correctly. Ram always reads 4 of 4

I'm getting a bunch of errors.

I put in some breakpoints to see why. I haven't traced it all the way back, but I see so far that after the ReadLn command on line 144, vMEMres is blank.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts