Jump to content

Remote Task Manager like


Recommended Posts

Hi. I am trying to create a remote task manager like using the WMI query. I am successful on getting list of running processes on remote PC with its Process ID, priority, user and executable path of the process. However what i am trying to get is the CPU usage and Memory usage of each corresponding process just like Windows task Manager. I am returning 0 values on my CPU and Memory usage field.

Can somebody help me on this? Is this possible?

Here is my script and i am stuck on how to get the CPU and Memory usage.

#Include <Constants.au3>
#include <GuiTreeView.au3>
#Include <GuiListView.au3>
#include <Array.au3>
#include <File.au3>
#Include <GuiTab.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GuiEdit.au3>
#include <File.au3>
#include <GUIListBox.au3>
#include <TabConstants.au3>
#include <Misc.au3>



Opt("WinTitleMatchMode", 2)
Opt("TrayIconHide", 1)

Global Const $Cursor_ARROW = 2
Global Const $wbemFlagReturnImmediately = 0x10
Global Const $wbemFlagForwardOnly = 0x20

$gui = GUICreate("Process List", 400, 350, -1, -1)
$host = GUICtrlCreateInput("Please enter computer name here...", 22, 30)
$end = GUICtrlCreateButton("End Process", 22, 300)
$Process = GUICtrlCreateTabItem("Processes")
$processes = GUICtrlCreateButton("Show Process", 22, 68, 90, 27)
$label = GUICtrlCreateLabel("", 130, 75, 250, 25)
$labelcount = GUICtrlCreateLabel("", 20, 115, 280, 25)
$listview = GUICtrlCreateListView("", 22, 135, 355, 126, BitOR($LVS_SORTASCENDING, $LVS_SINGLESEL))
_GUICtrlListView_AddColumn ($listview, "Process Name", 120)
_GUICtrlListView_AddColumn ($listview, "Username", 100)
_GUICtrlListView_AddColumn ($listview, "Process ID", 150)
_GUICtrlListView_AddColumn ($listview, "Priority", 120)
_GUICtrlListView_AddColumn ($listview, "Executable Path", 180)
_GUICtrlListView_AddColumn ($listview, "Command Parameters", 100)
_GUICtrlListView_AddColumn ($listview, "CPU Usage", 100)
_GUICtrlListView_AddColumn ($listview, "Memory Usage", 100)
GUISetState()

While 1
    
            $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        Exit
    EndIf
    
    
    If $msg = $processes Then
        _GUICtrlListView_DeleteAllItems($listview )
        GUICtrlSetData($label, "Getting list of running process on the computer....")
        $Process = _Process()
        $count = _GUICtrlListView_GetItemCount($listview)
        GUICtrlSetData($label, "")
        GUICtrlSetData($labelcount, "There are currently " & $count & " running processe(s) on computer.")      
    EndIf
    
    
    If $msg = $end Then 
            
        $msg = MsgBox(36, "Terminate Process", "Are you sure you want to terminate the process?")       

            If $msg = 6 Then                
                EndProcess()                
            EndIf       
    EndIf

Wend
    


Func _Process() 
    
    Local $sUserName, $sUserDomain
    Dim $Process[1][8], $i = 1  
    $strComputer = GUICtrlRead($host)
    $ping = Ping($strComputer)
    If $ping Then       
        $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
        $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)    

        If IsObj($colItems) Then
            For $objItem In $colItems
                ReDim $Process[UBound($Process) + 1][8]             
                $Process[$i][0] = $objItem.Name
                If $objItem.GetOwner($sUserName, $sUserDomain) = 0 Then $Process[$i][1] = $sUserDomain & "\" & $sUserName
                $Process[$i][2] = $objItem.ProcessId
                $Process[$i][3] = $objItem.Priority
                $Process[$i][4] = $objItem.ExecutablePath
                $Process[$i][5] = $objItem.CommandLine              
                
                If $objItem.GetOwner($sUserName, $sUserDomain) = 0 Then $Process[$i][5] = $sUserDomain & "\" & $sUserName
                
                $i += 1
            Next
                $Process[0][0] = UBound($Process) - 1
                If $Process[0][0] < 1 Then
                    SetError(1, 1, 0)
                EndIf
                Else
                    SetError(1, 2, 0)
                EndIf

        Local $oRefresher = ObjCreate("WbemScripting.SWbemRefresher")
        $colItems = $oRefresher.AddEnum ($objWMIService, "Win32_PerfFormattedData_PerfProc_Process" ).objectSet
        $oRefresher.Refresh
        
        ; Time delay before calling refresher
        Local $iTime = TimerInit()
        Do
            Sleep(10)
        Until TimerDiff($iTime) > 100
        $oRefresher.Refresh
        
        ; Get data
        For $objItem In $colItems
            ; Find it in the array
            For $n = 1 To $Process[0][0]
                If $Process[$n][1] = $objItem.IDProcess Then
                    $Process[$n][6] = $objItem.PercentProcessorTime
                    $Process[$n][7] = $objItem.WorkingSet
                    ExitLoop
                EndIf
            Next
        Next
    Else
        SetError(1) ; Error connecting to WMI
    EndIf
        

        For $i = 1 To UBound($Process) - 1
            $tmp = $Process[$i][0] & "|" & $Process[$i][1] & "|" & $Process[$i][2] & "|" & $Process[$i][3] & "|" & $Process[$i][4] _
            & "|" & $Process[$i][5] & "|" & $Process[$i][6] & "|" & Round($Process[$i][7] / 1024, 0) & " KB|"           
            GUICtrlCreateListViewItem($tmp, $listview)
        Next
    ;EndIf
EndFunc


Func EndProcess()   
    
    $string = _GUICtrlListView_GetItemTextArray($listview, -1)  
        
        
        For $i = 1 To 1         
            $PC = GUICtrlRead($host)
            $param = "\\" & $PC & " "
            $process = $string[$i] & " -t"  
            $execute = $param & $process
            $Run = Run("pskill.exe " & $execute, @SystemDir,@SW_HIDE)           
            Sleep(1500)
            _GUICtrlListView_DeleteItemsSelected($listview)
            $count = _GUICtrlListView_GetItemCount($listview)           
            MsgBox(64, "Process", $string[$i] & " has been terminated.")                        
        Next
            MsgBox(64, "", "There are currently " & $count & " running processes")
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...