Sign in to follow this  
Followers 0
Luka

Process information

11 posts in this topic

I want to get information for how long is some program running. (for example how long is notepad.exe running)

Does somebody have some ideas how to do it?

Best regards,

Luka

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MsgBox(0,"","By PID (unformatted): " & ProcessStartDateTime(ProcessExists("notepad.exe")) & @CRLF & "By Name (formatted): " & ProcessStartDateTime("notepad.exe", 1))

;PSDTid: PID or process name
;PSDTflag: 0 = Return unformatted date/time (YYYYMMDDHHMMSS.???), 1 = Return US formatted date/time (MM/DD/YYYY HH:MM:SS)
Func ProcessStartDateTime($PSDTid, $PSDTflag = 0)
    If NOT ProcessExists($PSDTid) Then SetError(1,1,0)    

    $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    
    If IsNumber($PSDTid) Then
        $PSDTquery = "SELECT * FROM Win32_Process WHERE ProcessId = " & $PSDTid
    Else
        $PSDTquery = "SELECT * FROM Win32_Process WHERE Name = '"  & $PSDTid & "'"
    EndIf
    
    $colItems = $objWMIService.ExecQuery($PSDTquery, "WQL", 0x10 + 0x20)

    If IsObj($colItems) then
        For $objItem In $colItems
            If $PSDTflag Then
                Return (StringMid($objItem.CreationDate, 5, 2) & "/" & StringMid($objItem.CreationDate, 7, 2) & "/" & StringLeft($objItem.CreationDate, 4) & " " & StringMid($objItem.CreationDate, 9, 2) & ":" & StringMid($objItem.CreationDate, 11, 2) & ":" & StringMid($objItem.CreationDate,13, 2))

            Else
                Return $objItem.CreationDate
            EndIf
        Next
    Else
       SetError(1,1,0)
    Endif
EndFunc

EDIT: Just a note, the unformatted return value is NOT compatible with TimerDiff()

Edited by weaponx

Share this post


Link to post
Share on other sites

@Luka

This would be an other way to monitor.

; Start Calc.exe after running this script
#include <Date.au3>

$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$objEventSource = $objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_Process'")


While 1
    $objEventObject = $objEventSource.NextEvent()
        If StringRight($objEventObject.TargetInstance.Name, 4) = ".exe" Then
        Select 
            Case $objEventObject.Path_.Class = "__InstanceCreationEvent"
                ConsoleWrite(  "Calc " & $objEventObject.TargetInstance.Name & " started: " & _Now()  & @LF)
            Case $objEventObject.Path_.Class = "__InstanceDeletionEvent"
                ConsoleWrite(  "Calc " & $objEventObject.TargetInstance.Name & " ended: " & _Now() & @LF)
        EndSelect
    EndIf
WEnd

Regards

ptrex

Share this post


Link to post
Share on other sites

ptrex, will this only monitor processes started after it is running?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@weaponx

It will start monitoring the process when it starts, and when it stops.

When you substract the two. You will have the uptime of that process you want to monitor.

regards

ptrex

No I meant, will it show the start time for processes theat were already running before this script is executed?

Edited by weaponx

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Yes. Since its just reading the existing process info from WMI, all that information is already stored. Yours is a good alternative for getting instant updates.

Share this post


Link to post
Share on other sites

@weaponx

I have added some Wmi2Au3 date Calculation function

#include <Date.au3>

MsgBox(0,"","By PID (unformatted): " & ProcessStartDateTime(ProcessExists("notepad.exe")) & @CRLF & "By Name (formatted): " & _
ProcessStartDateTime("notepad.exe", 1) )


;PSDTid: PID or process name
;PSDTflag: 0 = Return unformatted date/time (YYYYMMDDHHMMSS.???), 1 = Return US formatted date/time (MM/DD/YYYY HH:MM:SS)

Func ProcessStartDateTime($PSDTid, $PSDTflag = 0)
    If NOT ProcessExists($PSDTid) Then SetError(1,1,0)    

    $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    
    If IsNumber($PSDTid) Then
        $PSDTquery = "SELECT * FROM Win32_Process WHERE ProcessId = " & $PSDTid
    Else
        $PSDTquery = "SELECT * FROM Win32_Process WHERE Name = '"  & $PSDTid & "'"
    EndIf
    
    $colItems = $objWMIService.ExecQuery($PSDTquery, "WQL", 0x10 + 0x20)

    If IsObj($colItems) then
        For $objItem In $colItems
            If $PSDTflag Then
                ConsoleWrite("Formated : " & WMIDateStringToDate_AU3($objItem.CreationDate )& @LF)
                MsgBox( 4096, "", "Number of Seconds since : " &  _DateDiff( 's',WMIDateStringToDate_AU3($objItem.CreationDate),_NowCalc()) )
                MsgBox( 4096, "", "Number of Minutes since : " &  _DateDiff( 'n',WMIDateStringToDate_AU3($objItem.CreationDate),_NowCalc()) )
                MsgBox( 4096, "", "Number of Hours since : " &  _DateDiff( 'h',WMIDateStringToDate_AU3($objItem.CreationDate),_NowCalc()) )
                Return (StringMid($objItem.CreationDate, 5, 2) & "/" & StringMid($objItem.CreationDate, 7, 2) & "/" & _
                StringLeft($objItem.CreationDate, 4) & " " & StringMid($objItem.CreationDate, 9, 2) & ":" & _
                StringMid($objItem.CreationDate, 11, 2) & ":" & StringMid($objItem.CreationDate,13, 2))
                
            Else
                Return $objItem.CreationDate
            EndIf
        Next
    Else
       SetError(1,1,0)
    Endif
    
EndFunc

Func WMIDateStringToDate_AU3($dtmDate)
       Return (StringLeft($dtmDate, 4) & "/" & _
     StringMid($dtmDate, 5, 2) & "/" &  StringMid($dtmDate, 7, 2) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

Enjoy,

ptrex

Share this post


Link to post
Share on other sites

Sorry to reopen thread, just adding a cleaner version of my previous code. This returns a Date UDF compatible string.

;Return date udf compatible format
$startTime = ProcessStartDateTime("notepad.exe")

MsgBox(0,"",$startTime)

;PSDTid: PID or process name
Func ProcessStartDateTime($PSDTid)
    
    ;Convert to process id
    $PSDTid = ProcessExists($PSDTid)
    
    If NOT $PSDTid Then Return SetError(1,1,0)   

    $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    
    $PSDTquery = "SELECT * FROM Win32_Process WHERE ProcessId = " & $PSDTid
   
    $colItems = $objWMIService.ExecQuery($PSDTquery, "WQL", 0x10 + 0x20)

    If IsObj($colItems) then
        For $objItem In $colItems
            ;Return start date
            $tDate = $objItem.CreationDate
            Return StringFormat("%s/%s/%s %s:%s:%s",StringLeft($tDate, 4),StringMid($tDate, 5, 2),StringMid($tDate, 7, 2),StringMid($tDate, 9, 2), StringMid($tDate, 11, 2),StringMid($tDate,13, 2))
        Next
    Else
       Return SetError(1,1,0)
    Endif
EndFunc

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