Sign in to follow this  
Followers 0
JohnRichard

Help with Array not returning values

5 posts in this topic

hi all. i have this script which queries processes on remote computer using WMI. The script has tow WMI values to search for Win32_process and Win32_PerfFormattedData_PerfProc_Process. I am able to query all the values on Win32_Process and successfully return the values in array listed in listview. But on the Win32_PerfFormattedData_PerfProc_Process, the script does not return any value that i wanted to show in the listview. All in all i wanted to combine the values of two WMI request into the listview array.

Can someone help me here. Or if you guys know other way to work on this script, i'll appreciate much all your help.

i'm stuck up to this point. here is the code:

#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)
$Process = GUICtrlCreateTabItem("Processes")
$processes = GUICtrlCreateButton("Show Process", 22, 68, 90, 27)
$listview = GUICtrlCreateListView("", 22, 135, 355, 126)
_GUICtrlListView_AddColumn ($listview, "Process Name", 120)
_GUICtrlListView_AddColumn ($listview, "Executable Path", 180)
_GUICtrlListView_AddColumn ($listview, "Command Parameters", 100)
_GUICtrlListView_AddColumn ($listview, "Priority", 120)
_GUICtrlListView_AddColumn ($listview, "Process ID", 150)
_GUICtrlListView_AddColumn ($listview, "Username", 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
        _Process()  
    EndIf

Wend
    

Func _Process()
    
    Local $sUserName, $sUserDomain
    Dim $Process[1][8], $i = 1
;$strComputer = (GUICtrlRead($hostlist))
    $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)    

        Local $oRefresher = ObjCreate("WbemScripting.SWbemRefresher")
            $objItem = $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 

        If IsObj($colItems) Then
            For $objItem In $colItems
                ReDim $Process[UBound($Process) + 1][8]             
                $Process[$i][0] = $objItem.Caption
                $Process[$i][1] = $objItem.ExecutablePath
                $Process[$i][2] = $objItem.CommandLine   
                $Process[$i][3] = $objItem.Priority
                $Process[$i][4] = $objItem.ProcessId
                
                If $objItem.GetOwner($sUserName, $sUserDomain) = 0 Then $Process[$i][5] = $sUserDomain & "\" & $sUserName               
            
                
            ; Get PerfProc data             
                For $objItem In $objWMIService
            ; Find it in the array
                    For $i = 1 To $Process[0][0]
                        If $Process[$i][1] = $objItem.IDProcess Then
                    ; [n][6] = CPU usage
                            $Process[$i][6] = $objItem.PercentProcessorTime
                    ; [n][7] = memory usage
                            $Process[$i][7] = $objItem.WorkingSet
                            ExitLoop
                        EndIf
                    Next
                Next

  ; Else
       ;SetError(1); Error connecting to WMI
   ;EndIf                               
                                
                $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           
        

        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

Share this post


Link to post
Share on other sites



can someone help me here. i just wanted to combine the CPU and Memory Usage values into the listview array.

Share this post


Link to post
Share on other sites

It's incredibly rude to bump your post within 24 hours of your previous post. Someone will help you when they have the time. As for your problem I haven't looked through it all that well but for your While loop you shouldn't use If statements, try instead using Switch $msg/ Case statments.

Share this post


Link to post
Share on other sites

hi all. i have this script which queries processes on remote computer using WMI. The script has tow WMI values to search for Win32_process and Win32_PerfFormattedData_PerfProc_Process. I am able to query all the values on Win32_Process and successfully return the values in array listed in listview. But on the Win32_PerfFormattedData_PerfProc_Process, the script does not return any value that i wanted to show in the listview. All in all i wanted to combine the values of two WMI request into the listview array.

Can someone help me here. Or if you guys know other way to work on this script, i'll appreciate much all your help.

i'm stuck up to this point. here is the code:

CODE
#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)

$Process = GUICtrlCreateTabItem("Processes")

$processes = GUICtrlCreateButton("Show Process", 22, 68, 90, 27)

$listview = GUICtrlCreateListView("", 22, 135, 355, 126)

_GUICtrlListView_AddColumn ($listview, "Process Name", 120)

_GUICtrlListView_AddColumn ($listview, "Executable Path", 180)

_GUICtrlListView_AddColumn ($listview, "Command Parameters", 100)

_GUICtrlListView_AddColumn ($listview, "Priority", 120)

_GUICtrlListView_AddColumn ($listview, "Process ID", 150)

_GUICtrlListView_AddColumn ($listview, "Username", 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

_Process()

EndIf

Wend

Func _Process()

Local $sUserName, $sUserDomain

Dim $Process[1][8], $i = 1

;$strComputer = (GUICtrlRead($hostlist))

$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)

Local $oRefresher = ObjCreate("WbemScripting.SWbemRefresher")

$objItem = $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

If IsObj($colItems) Then

For $objItem In $colItems

ReDim $Process[uBound($Process) + 1][8]

$Process[$i][0] = $objItem.Caption

$Process[$i][1] = $objItem.ExecutablePath

$Process[$i][2] = $objItem.CommandLine

$Process[$i][3] = $objItem.Priority

$Process[$i][4] = $objItem.ProcessId

If $objItem.GetOwner($sUserName, $sUserDomain) = 0 Then $Process[$i][5] = $sUserDomain & "\" & $sUserName

; Get PerfProc data

For $objItem In $objWMIService

; Find it in the array

For $i = 1 To $Process[0][0]

If $Process[$i][1] = $objItem.IDProcess Then

; [n][6] = CPU usage

$Process[$i][6] = $objItem.PercentProcessorTime

; [n][7] = memory usage

$Process[$i][7] = $objItem.WorkingSet

ExitLoop

EndIf

Next

Next

; Else

;SetError(1); Error connecting to WMI

;EndIf

$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

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

You are reusing $objItem while it is still being used for something else. Also, your collection from the refresher needs to be enumerated by a For/Next loop, but you saved it as $objItem again.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

so how will i do it then? i tried to change the $objitem to different name like $objitem2 in the refresher but it does't return any values also.

Share this post


Link to post
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
Sign in to follow this  
Followers 0