Jump to content

Obtain boot time with AutoIT


Kate
 Share

Recommended Posts

requires beta, but i think this is what your looking for (can be done remotely, just replace @ComputerName with the name of the computer)

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = @ComputerName

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_System", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
        $uptime = $objItem.SystemUpTime
        If $uptime > 60 Then 
            $uptime = $uptime / 60; convert the seconds to minutes
            If $uptime > 60 Then 
                $uptime = $uptime / 60; convert the minutes to hours
                If $uptime > 24 Then; convert hours to days
                    $uptime = Number(StringFormat("%.2f",$uptime / 24))
                    If $uptime > 1.0 Then; uptime more than 1 day
                        $Output = $Output & "SystemUpTime: " & $uptime & @CRLF
                        if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
                    EndIf
                EndIf
            EndIf
        EndIf
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

$ret = DllCall("kernel32.dll","long","GetTickCount")
If IsArray($ret) Then
    $uptime = $ret[0] / 1000; convert miliseconds to seconds
    If $uptime > 60 Then 
        $uptime = $uptime / 60; convert the seconds to minutes
        If $uptime > 60 Then 
            $uptime = $uptime / 60; convert the minutes to hours
            If $uptime > 24 Then; convert hours to days
                $uptime = Number(StringFormat("%.2f",$uptime / 24))
                If $uptime > 1.0 Then; uptime more than 1 day
                    MsgBox(0,"Uptime", $uptime)
                EndIf
            EndIf
        EndIf
    EndIf
EndIf

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

I had to move the msgbox to get an output since uptime was not more than 24 hours.

$ret = DllCall("kernel32.dll","long","GetTickCount")

; convert miliseconds to seconds
If IsArray($ret) Then $uptime = $ret[0] / 1000

; convert the seconds to minutes
If $uptime > 60 Then $uptime = $uptime / 60

; convert the minutes to hours
If $uptime > 60 Then $uptime = $uptime / 60

; convert hours to days
If $uptime > 24 Then $uptime = Number(StringFormat("%.2f",$uptime / 24))

MsgBox(0,"Uptime", $uptime)
What formatting did you have in mind if greater than one day?

...and thanks for the code...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

I had to move the msgbox to get an output since uptime was not more than 24 hours.

$ret = DllCall("kernel32.dll","long","GetTickCount")

; convert miliseconds to seconds
If IsArray($ret) Then $uptime = $ret[0] / 1000

; convert the seconds to minutes
If $uptime > 60 Then $uptime = $uptime / 60

; convert the minutes to hours
If $uptime > 60 Then $uptime = $uptime / 60

; convert hours to days
If $uptime > 24 Then $uptime = Number(StringFormat("%.2f",$uptime / 24))

MsgBox(0,"Uptime", $uptime)
What formatting did you have in mind if greater than one day?

...and thanks for the code...

Leave your system on for more than a day and see what it returns, on my system right now it returned 3.93

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

$ret = DllCall("kernel32.dll", "long", "GetTickCount")
If IsArray($ret) Then
   $type = "Seconds"
   $uptime = Number(StringFormat("%.2f", $ret[0] / 1000)); convert miliseconds to seconds
   If $uptime > 60 Then
      $type = "Minute(s)"
      $uptime = Number(StringFormat("%.2f", $uptime / 60)); convert the seconds to minutes
      If $uptime > 60 Then
         $type = "Hour(s)"
         $uptime = Number(StringFormat("%.2f", $uptime / 60)); convert the minutes to hours
         If $uptime > 24 Then; convert hours to days
            $type = "Day(s)"
            $uptime = Number(StringFormat("%.2f", $uptime / 24))
         EndIf
      EndIf
   EndIf
EndIf
MsgBox(0, "Uptime", $uptime & " " & $type)

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

incase you like another formating

$ret = DllCall("kernel32.dll", "long", "GetTickCount")
If IsArray($ret) Then
   $msec=StringRight("00" &  mod($ret[0],1000),3)
   $uptime=floor($ret[0]/1000)
   $sec=StringRight("00" & mod($uptime,60),2)
   If $uptime >= 60 Then
      $uptime= floor($uptime/60)
      $min=StringRight("00" & mod($uptime,60),2)
      If $uptime >= 60 Then
        $uptime=floor($uptime/60)
        $hour = StringRight("00" & mod($uptime,24),2)
         If $uptime >= 24 Then; convert hours to days
            $day=floor($uptime/24)
            $type=""
            if $day >1 then $type = "s"
            $type = " Day" & $type & " "
         EndIf
      EndIf
   EndIf
MsgBox(0, "Uptime", $day & $type &  $hour & ":" & $min &":"& $sec & '.'& $msec)
EndIf

Link to comment
Share on other sites

  • 4 years later...

Just a shorter way to do the same thing.

$ret = DllCall("kernel32.dll","long","GetTickCount")

If IsArray($ret) Then
    $uptime = Round(($ret[0] / 3600000),1); convert miliseconds to hours
    MsgBox(0,"Uptime", $uptime,3)
EndIf

A little help needed..

How would you use the dllcall function to check uptime on a remote machine?

Thanks

Link to comment
Share on other sites

On remote machines? I think WMI is the best way to go...requires of course active WMI and sufficient access rights on remote machine.

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output &= "Computer: " & $strComputer  & @CRLF
$Output &= "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_System", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
    For $objItem In $colItems
        $Output &= "SystemUpTime: " & $objItem.SystemUpTime ; in seconds
        if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
        $Output=""
    Next
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PerfFormattedData_PerfOS_System" )
Endif
Link to comment
Share on other sites

  • 13 years later...

Hi all,

sorry if i jump into this thread a bit late <bg>

Just about formating into:

MsgBox(0, "Uptime", $day & $type &  $hour & ":" & $min &":"& $sec & '.'& $msec)

If I use:

_WinAPI_GetDurationFormat(0, $ret[0] * 1000 * 10)

I get slightly different values (2 sec difference)

Which format procedure should I trust?

Cheers mike

Link to comment
Share on other sites

  • Developers

How are those variables filled?
Show a short script that is runnable showing your issue....

 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

With this code I obtain exactly the same strings :

#include <WinAPISys.au3>
#include <WinAPILocale.au3>

Local $iTime = _WinAPI_GetTickCount()
$sTime = _WinAPI_GetDurationFormat($LOCALE_USER_DEFAULT, $iTime * 10000, "hh:mm:ss")
ConsoleWrite($sTime & @CRLF)
$iTime = Int($iTime/1000)
Local $iSec = Mod($iTime, 60)
$iTime = Int($iTime / 60)
Local $iMin = Mod($iTime, 60)
Local $iHour = Int($iTime / 60)
ConsoleWrite(StringFormat("%02i:%02i:%02i", $iHour, $iMin, $iSec) & @CRLF)

 

Edited by Nine
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...